<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <title>Requests 1.1.0 文档</title>

    <link rel="stylesheet" href="_static/flasky.css?@cache1" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css?@cache1" type="text/css" />

    <link rel="top" title="None" href="index.html#document-index" />



  <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9">


  </head>
  <body role="document">
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul
        <li class="nav-item nav-item-0"><a href="index.html#document-index">Requests 1.1.0 文档</a> &raquo;</li>
      </ul>
    </div>

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">

  <div class="section" id="requests-http-for-humans">
<h1>Requests: HTTP for Humans<a class="headerlink" href="#requests-http-for-humans" title="永久链接至标题">¶</a></h1>
<p>发行版本 v1.1.0. (<a class="reference internal" href="index.html#install"><span>安装</span></a>)</p>
<p>Requests 是使用 <a class="reference internal" href="index.html#apache2"><span>Apache2 Licensed</span></a> 许可证的 HTTP 库。用 Python 编写，真正的为人类着想。</p>
<p>Python 标准库中的 <strong>urllib2</strong> 模块提供了你所需要的大多数 HTTP 功能，但是它的 API 太渣了。它是为另一个时代、另一个互联网所创建的。它需要巨量的工作，甚至包括各种方法覆盖，来完成最简单的任务。</p>
<p>在Python的世界里，事情不应该这么麻烦。</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;https://api.github.com/user&#39;</span><span class="p">,</span> <span class="n">auth</span><span class="o">=</span><span class="p">(</span><span class="s">&#39;user&#39;</span><span class="p">,</span> <span class="s">&#39;pass&#39;</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">status_code</span>
<span class="go">200</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">headers</span><span class="p">[</span><span class="s">&#39;content-type&#39;</span><span class="p">]</span>
<span class="go">&#39;application/json; charset=utf8&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">encoding</span>
<span class="go">&#39;utf-8&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">text</span>
<span class="go">u&#39;{&quot;type&quot;:&quot;User&quot;...&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">json</span><span class="p">()</span>
<span class="go">{u&#39;private_gists&#39;: 419, u&#39;total_private_repos&#39;: 77, ...}</span>
</pre></div>
</div>
<p>参见 <a class="reference external" href="https://gist.github.com/973705">未使用 Requests 的相似代码</a>.</p>
<p>Requests 使用的是 urllib3，因此继承了它的所有特性。Requests 支持 HTTP 连接保持和连接池，支持使用 cookie 保持会话，支持文件上传，支持自动确定响应内容的编码，支持国际化的 URL 和 POST 数据自动编码。现代、国际化、人性化。</p>
<div class="section" id="testimonials">
<h2>Testimonials<a class="headerlink" href="#testimonials" title="永久链接至标题">¶</a></h2>
<p>Her Majesty&#8217;s Government, Amazon, Google, Twilio, Mozilla, Heroku, PayPal, NPR, Obama for America, Transifex, Native Instruments, The Washington Post, Twitter, SoundCloud, Kippt, Readability, and Federal US Institutions use Requests internally. It has been downloaded over 2,000,000 times from PyPI.</p>
<dl class="docutils">
<dt><strong>Armin Ronacher</strong></dt>
<dd>Requests is the perfect example how beautiful an API can be with the
right level of abstraction.</dd>
<dt><strong>Matt DeBoard</strong></dt>
<dd>I&#8217;m going to get &#64;kennethreitz&#8217;s Python requests module tattooed
on my body, somehow. The whole thing.</dd>
<dt><strong>Daniel Greenfeld</strong></dt>
<dd>Nuked a 1200 LOC spaghetti code library with 10 lines of code thanks to
&#64;kennethreitz&#8217;s request library. Today has been AWESOME.</dd>
<dt><strong>Kenny Meyers</strong></dt>
<dd>Python HTTP: When in doubt, or when not in doubt, use Requests. Beautiful,
simple, Pythonic.</dd>
</dl>
</div>
<div class="section" id="id1">
<h2>功能特性<a class="headerlink" href="#id1" title="永久链接至标题">¶</a></h2>
<p>Requests 完全满足如今网络的需求。</p>
<ul class="simple">
<li>国际化域名和 URLs</li>
<li>Keep-Alive &amp; 连接池</li>
<li>持久的 Cookie 会话</li>
<li>类浏览器式的 SSL 加密认证</li>
<li>基本/摘要式的身份认证</li>
<li>优雅的键/值 Cookies</li>
<li>自动解压</li>
<li>Unicode 编码的响应体</li>
<li>多段文件上传</li>
<li>连接超时</li>
<li>支持 <code class="docutils literal"><span class="pre">.netrc</span></code></li>
<li>适用于 Python 2.6—3.4</li>
<li>线程安全</li>
</ul>
</div>
<div class="section" id="id2">
<h2>用户指南<a class="headerlink" href="#id2" title="永久链接至标题">¶</a></h2>
<p>这部分文档主要介绍了 Requests 的背景，然后对于 Requests 的应用做了一步一步的要点介绍。</p>
<div class="toctree-wrapper compound">
<span id="document-user/intro"></span><div class="section" id="introduction">
<span id="id1"></span><h3>简介<a class="headerlink" href="#introduction" title="永久链接至标题">¶</a></h3>
<div class="section" id="id2">
<h4>开发哲学<a class="headerlink" href="#id2" title="永久链接至标题">¶</a></h4>
<p>Requests 是以 <span class="target" id="index-0"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0020"><strong>PEP 20</strong></a> 的习语为中心开发的</p>
<ol class="arabic simple">
<li>Beautiful is better than ugly.(美丽优于丑陋)</li>
<li>Explicit is better than implicit.(清楚优于含糊)</li>
<li>Simple is better than complex.(简单优于复杂)</li>
<li>Complex is better than complicated.(复杂优于繁琐)</li>
<li>Readability counts.(重要的是可读性)</li>
</ol>
<p>对于 Requests 所有的贡献都应牢记这些重要的准则。</p>
</div>
<div class="section" id="apache2">
<span id="id3"></span><h4>Apache2 协议<a class="headerlink" href="#apache2" title="永久链接至标题">¶</a></h4>
<p>现在你找到的许多开源项目都是以 <a class="reference external" href="http://www.opensource.org/licenses/gpl-license.php">GPL Licensed</a> 发布的.
虽然 GPL 有它自己的时间和空间，但应该确定的是它不会是你下一个开源项目的发布协议。</p>
<p>即使一个项目发行于GPL协议也不能用于任何本身没开源的商业产品。</p>
<p>The MIT, BSD, ISC, and Apache2 licenses are great alternatives to the GPL
that allow your open-source software to be used freely in proprietary,
closed-source software.</p>
<p>Requests is released under terms of <a class="reference external" href="http://opensource.org/licenses/Apache-2.0">Apache2 License</a>.</p>
</div>
<div class="section" id="requests">
<h4>Requests 协议<a class="headerlink" href="#requests" title="永久链接至标题">¶</a></h4>
<blockquote>
<div><p>Copyright 2013 Kenneth Reitz</p>
<blockquote>
<div><p>Licensed under the Apache License, Version 2.0 (the &#8220;License&#8221;);
you may not use this file except in compliance with the License.
You may obtain a copy of the License at</p>
<blockquote>
<div><a class="reference external" href="http://www.apache.org/licenses/LICENSE-2.0">http://www.apache.org/licenses/LICENSE-2.0</a></div></blockquote>
<p>Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an &#8220;AS IS&#8221; BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.</p>
</div></blockquote>
</div></blockquote>
</div>
</div>
<span id="document-user/install"></span><div class="section" id="install">
<span id="id1"></span><h3>安装<a class="headerlink" href="#install" title="永久链接至标题">¶</a></h3>
<p>这部分文档包含了Requests的安装过程，使用任何软件的第一步就是合适的安装好它.</p>
<div class="section" id="distribute-pip">
<h4>Distribute &amp; Pip<a class="headerlink" href="#distribute-pip" title="永久链接至标题">¶</a></h4>
<p>使用 <a class="reference external" href="http://www.pip-installer.org/">pip</a> 安装Requests非常简单</p>
<div class="highlight-python"><div class="highlight"><pre>$ pip install requests
</pre></div>
</div>
<p>或者使用 <a class="reference external" href="http://pypi.python.org/pypi/setuptools">easy_install</a> 安装</p>
<div class="highlight-python"><div class="highlight"><pre>$ easy_install requests
</pre></div>
</div>
<p>但是你最好 <a class="reference external" href="http://www.pip-installer.org/en/latest/other-tools.html#pip-compared-to-easy-install">不要这样</a>.</p>
</div>
<div class="section" id="id3">
<h4>获得源码<a class="headerlink" href="#id3" title="永久链接至标题">¶</a></h4>
<p>Requests 一直在Github上被积极的开发着，你可以在此获得代码，且总是
<a class="reference external" href="https://github.com/kennethreitz/requests">可用的</a>.</p>
<p>你也可以克隆公共版本库:</p>
<div class="highlight-python"><div class="highlight"><pre>git clone git://github.com/kennethreitz/requests.git
</pre></div>
</div>
<p>下载 <a class="reference external" href="https://github.com/kennethreitz/requests/tarball/master">源码</a>:</p>
<div class="highlight-python"><div class="highlight"><pre>$ curl -OL https://github.com/kennethreitz/requests/tarball/master
</pre></div>
</div>
<p>或者下载 <a class="reference external" href="https://github.com/kennethreitz/requests/zipball/master">zipball</a>:</p>
<div class="highlight-python"><div class="highlight"><pre>$ curl -OL https://github.com/kennethreitz/requests/zipball/master
</pre></div>
</div>
<p>一旦你获得了复本，你就可以轻松的将它嵌入到你的python包里或者安装到你的site-packages:</p>
<div class="highlight-python"><div class="highlight"><pre>$ python setup.py install
</pre></div>
</div>
</div>
</div>
<span id="document-user/quickstart"></span><div class="section" id="module-requests.models">
<span id="id1"></span><span id="quickstart"></span><h3>快速上手<a class="headerlink" href="#module-requests.models" title="永久链接至标题">¶</a></h3>
<p>迫不及待了吗？本页内容为如何入门Requests提供了很好的指引。其假设你已经安装了Requests。如果还没有，
去 <a class="reference internal" href="index.html#install"><span>安装</span></a> 一节看看吧。</p>
<p>首先，确认一下：</p>
<ul class="simple">
<li>Requests <a class="reference internal" href="index.html#install"><span>已安装</span></a></li>
<li>Requests是 <a class="reference internal" href="index.html#updates"><span>最新的</span></a></li>
</ul>
<p>让我们从一些简单的示例开始吧。</p>
<div class="section" id="id2">
<h4>发送请求<a class="headerlink" href="#id2" title="永久链接至标题">¶</a></h4>
<p>使用Requests发送网络请求非常简单。</p>
<p>一开始要导入Requests模块:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">requests</span>
</pre></div>
</div>
<p>然后，尝试获取某个网页。本例子中，我们来获取Github的公共时间线</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;https://github.com/timeline.json&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>现在，我们有一个名为 <code class="docutils literal"><span class="pre">r</span></code> 的 <a class="reference internal" href="index.html#requests.Response" title="requests.Response"><code class="xref py py-class docutils literal"><span class="pre">Response</span></code></a> 对象。可以从这个对象中获取所有我们想要的信息。</p>
<p>Requests简便的API意味着所有HTTP请求类型都是显而易见的。例如，你可以这样发送一个HTTP POST请求:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">post</span><span class="p">(</span><span class="s">&quot;http://httpbin.org/post&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>漂亮，对吧？那么其他HTTP请求类型：PUT， DELETE， HEAD以及OPTIONS又是如何的呢？都是一样的简单:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">put</span><span class="p">(</span><span class="s">&quot;http://httpbin.org/put&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">delete</span><span class="p">(</span><span class="s">&quot;http://httpbin.org/delete&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">head</span><span class="p">(</span><span class="s">&quot;http://httpbin.org/get&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">options</span><span class="p">(</span><span class="s">&quot;http://httpbin.org/get&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>都很不错吧，但这也仅是Requests的冰山一角呢。</p>
</div>
<div class="section" id="url">
<h4>为URL传递参数<a class="headerlink" href="#url" title="永久链接至标题">¶</a></h4>
<p>你也许经常想为URL的查询字符串(query string)传递某种数据。如果你是手工构建URL，那么数据会以键/值
对的形式置于URL中，跟在一个问号的后面。例如， <code class="docutils literal"><span class="pre">httpbin.org/get?key=val</span></code> 。
Requests允许你使用 <code class="docutils literal"><span class="pre">params</span></code> 关键字参数，以一个字典来提供这些参数。举例来说，如果你想传递
<code class="docutils literal"><span class="pre">key1=value1</span></code> 和 <code class="docutils literal"><span class="pre">key2=value2</span></code> 到 <code class="docutils literal"><span class="pre">httpbin.org/get</span></code> ，那么你可以使用如下代码:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">payload</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;key1&#39;</span><span class="p">:</span> <span class="s">&#39;value1&#39;</span><span class="p">,</span> <span class="s">&#39;key2&#39;</span><span class="p">:</span> <span class="s">&#39;value2&#39;</span><span class="p">}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&quot;http://httpbin.org/get&quot;</span><span class="p">,</span> <span class="n">params</span><span class="o">=</span><span class="n">payload</span><span class="p">)</span>
</pre></div>
</div>
<p>通过打印输出该URL，你能看到URL已被正确编码:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">print</span><span class="p">(</span><span class="n">r</span><span class="o">.</span><span class="n">url</span><span class="p">)</span>
<span class="go">http://httpbin.org/get?key2=value2&amp;key1=value1</span>
</pre></div>
</div>
<p>注意字典里值为 <code class="docutils literal"><span class="pre">None</span></code> 的键都不会被添加到 URL 的查询字符串里。</p>
</div>
<div class="section" id="id3">
<h4>响应内容<a class="headerlink" href="#id3" title="永久链接至标题">¶</a></h4>
<p>我们能读取服务器响应的内容。再次以Github时间线为例:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">requests</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;https://github.com/timeline.json&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">text</span>
<span class="go">u&#39;[{&quot;repository&quot;:{&quot;open_issues&quot;:0,&quot;url&quot;:&quot;https://github.com/...</span>
</pre></div>
</div>
<p>Requests会自动解码来自服务器的内容。大多数unicode字符集都能被无缝地解码。</p>
<p>请求发出后，Requests会基于HTTP头部对响应的编码作出有根据的推测。当你访问 <code class="docutils literal"><span class="pre">r.text</span></code>
之时，Requests会使用其推测的文本编码。你可以找出Requests使用了什么编码，并且能够使用
<code class="docutils literal"><span class="pre">r.encoding</span></code> 属性来改变它:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">encoding</span>
<span class="go">&#39;utf-8&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">encoding</span> <span class="o">=</span> <span class="s">&#39;ISO-8859-1&#39;</span>
</pre></div>
</div>
<p>如果你改变了编码，每当你访问 <code class="docutils literal"><span class="pre">r.text</span></code> ，Request都将会使用 <code class="docutils literal"><span class="pre">r.encoding</span></code> 的新值。你可能希望在使用特殊逻辑计算出文本的编码的情况下来修改编码。比如 HTTP 和 XML 自身可以指定编码。这样的话，你应该使用 <code class="docutils literal"><span class="pre">r.content</span></code> 来找到编码，然后设置 <code class="docutils literal"><span class="pre">r.encoding</span></code> 为相应的编码。这样就能使用正确的编码解析 <code class="docutils literal"><span class="pre">r.text</span></code> 了。</p>
<p>在你需要的情况下，Requests也可以使用定制的编码。如果你创建了自己的编码，并使用
<code class="docutils literal"><span class="pre">codecs</span></code> 模块进行注册，你就可以轻松地使用这个解码器名称作为 <code class="docutils literal"><span class="pre">r.encoding</span></code> 的值，
然后由Requests来为你处理编码。</p>
</div>
<div class="section" id="id4">
<h4>二进制响应内容<a class="headerlink" href="#id4" title="永久链接至标题">¶</a></h4>
<p>你也能以字节的方式访问请求响应体，对于非文本请求:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">content</span>
<span class="go">b&#39;[{&quot;repository&quot;:{&quot;open_issues&quot;:0,&quot;url&quot;:&quot;https://github.com/...</span>
</pre></div>
</div>
<p>Requests会自动为你解码 <code class="docutils literal"><span class="pre">gzip</span></code> 和 <code class="docutils literal"><span class="pre">deflate</span></code> 传输编码的响应数据。</p>
<p>例如，以请求返回的二进制数据创建一张图片，你可以使用如下代码:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">PIL</span> <span class="kn">import</span> <span class="n">Image</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">StringIO</span> <span class="kn">import</span> <span class="n">StringIO</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">i</span> <span class="o">=</span> <span class="n">Image</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="n">r</span><span class="o">.</span><span class="n">content</span><span class="p">))</span>
</pre></div>
</div>
</div>
<div class="section" id="json">
<h4>JSON响应内容<a class="headerlink" href="#json" title="永久链接至标题">¶</a></h4>
<p>Requests中也有一个内置的JSON解码器，助你处理JSON数据:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">requests</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;https://github.com/timeline.json&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">json</span><span class="p">()</span>
<span class="go">[{u&#39;repository&#39;: {u&#39;open_issues&#39;: 0, u&#39;url&#39;: &#39;https://github.com/...</span>
</pre></div>
</div>
<p>如果JSON解码失败， <code class="docutils literal"><span class="pre">r.json</span></code> 就会抛出一个异常。例如，相应内容是 401 (Unauthorized) ，尝试访问 <code class="docutils literal"><span class="pre">r.json</span></code> 将会抛出 <code class="docutils literal"><span class="pre">ValueError:</span>
<span class="pre">No</span> <span class="pre">JSON</span> <span class="pre">object</span> <span class="pre">could</span> <span class="pre">be</span> <span class="pre">decoded</span></code> 异常。</p>
</div>
<div class="section" id="id5">
<h4>原始响应内容<a class="headerlink" href="#id5" title="永久链接至标题">¶</a></h4>
<p>在罕见的情况下你可能想获取来自服务器的原始套接字响应，那么你可以访问 <code class="docutils literal"><span class="pre">r.raw</span></code> 。
如果你确实想这么干，那请你确保在初始请求中设置了 <code class="docutils literal"><span class="pre">stream=True</span></code> 。具体的你可以这么做:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;https://github.com/timeline.json&#39;</span><span class="p">,</span> <span class="n">stream</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">raw</span>
<span class="go">&lt;requests.packages.urllib3.response.HTTPResponse object at 0x101194810&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">raw</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
<span class="go">&#39;\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03&#39;</span>
</pre></div>
</div>
<p>但一般情况下，你应该下面的模式将文本流保存到文件:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="s">&#39;wb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">fd</span><span class="p">:</span>
    <span class="k">for</span> <span class="n">chunk</span> <span class="ow">in</span> <span class="n">r</span><span class="o">.</span><span class="n">iter_content</span><span class="p">(</span><span class="n">chunk_size</span><span class="p">):</span>
        <span class="n">fd</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">chunk</span><span class="p">)</span>
</pre></div>
</div>
<p>使用 <code class="docutils literal"><span class="pre">Response.iter_content</span></code> 将会处理大量你直接使用 <code class="docutils literal"><span class="pre">Response.raw</span></code> 不得不处理的。
当流下载时，上面是优先推荐的获取内容方式。</p>
</div>
<div class="section" id="id6">
<h4>定制请求头<a class="headerlink" href="#id6" title="永久链接至标题">¶</a></h4>
<p>如果你想为请求添加HTTP头部，只要简单地传递一个 <code class="docutils literal"><span class="pre">dict</span></code> 给 <code class="docutils literal"><span class="pre">headers</span></code> 参数就可以了。</p>
<p>例如，在前一个示例中我们没有指定content-type:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">json</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">url</span> <span class="o">=</span> <span class="s">&#39;https://api.github.com/some/endpoint&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">payload</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;some&#39;</span><span class="p">:</span> <span class="s">&#39;data&#39;</span><span class="p">}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">headers</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;content-type&#39;</span><span class="p">:</span> <span class="s">&#39;application/json&#39;</span><span class="p">}</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">post</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">payload</span><span class="p">),</span> <span class="n">headers</span><span class="o">=</span><span class="n">headers</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="post">
<h4>更加复杂的POST请求<a class="headerlink" href="#post" title="永久链接至标题">¶</a></h4>
<p>通常，你想要发送一些编码为表单形式的数据&#8212;非常像一个HTML表单。
要实现这个，只需简单地传递一个字典给 <cite>data</cite> 参数。你的数据字典
在发出请求时会自动编码为表单形式:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">payload</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;key1&#39;</span><span class="p">:</span> <span class="s">&#39;value1&#39;</span><span class="p">,</span> <span class="s">&#39;key2&#39;</span><span class="p">:</span> <span class="s">&#39;value2&#39;</span><span class="p">}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">post</span><span class="p">(</span><span class="s">&quot;http://httpbin.org/post&quot;</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="n">payload</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">r</span><span class="o">.</span><span class="n">text</span>
<span class="go">{</span>
<span class="go">  ...</span>
<span class="go">  &quot;form&quot;: {</span>
<span class="go">    &quot;key2&quot;: &quot;value2&quot;,</span>
<span class="go">    &quot;key1&quot;: &quot;value1&quot;</span>
<span class="go">  },</span>
<span class="go">  ...</span>
<span class="go">}</span>
</pre></div>
</div>
<p>很多时候你想要发送的数据并非编码为表单形式的。如果你传递一个 <code class="docutils literal"><span class="pre">string</span></code> 而不是一个 <code class="docutils literal"><span class="pre">dict</span></code> ，那么数据会被直接发布出去。</p>
<p>例如，Github API v3接受编码为JSON的POST/PATCH数据:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">json</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">url</span> <span class="o">=</span> <span class="s">&#39;https://api.github.com/some/endpoint&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">payload</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;some&#39;</span><span class="p">:</span> <span class="s">&#39;data&#39;</span><span class="p">}</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">post</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">payload</span><span class="p">))</span>
</pre></div>
</div>
</div>
<div class="section" id="post-multipart-encoded">
<h4>POST一个多部分编码(Multipart-Encoded)的文件<a class="headerlink" href="#post-multipart-encoded" title="永久链接至标题">¶</a></h4>
<p>Requests使得上传多部分编码文件变得很简单:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">url</span> <span class="o">=</span> <span class="s">&#39;http://httpbin.org/post&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">files</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;file&#39;</span><span class="p">:</span> <span class="nb">open</span><span class="p">(</span><span class="s">&#39;report.xls&#39;</span><span class="p">,</span> <span class="s">&#39;rb&#39;</span><span class="p">)}</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">post</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">files</span><span class="o">=</span><span class="n">files</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">text</span>
<span class="go">{</span>
<span class="go">  ...</span>
<span class="go">  &quot;files&quot;: {</span>
<span class="go">    &quot;file&quot;: &quot;&lt;censored...binary...data&gt;&quot;</span>
<span class="go">  },</span>
<span class="go">  ...</span>
<span class="go">}</span>
</pre></div>
</div>
<p>你可以显式地设置文件名，文件类型和请求头:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">url</span> <span class="o">=</span> <span class="s">&#39;http://httpbin.org/post&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">files</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;file&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s">&#39;report.xls&#39;</span><span class="p">,</span> <span class="nb">open</span><span class="p">(</span><span class="s">&#39;report.xls&#39;</span><span class="p">,</span> <span class="s">&#39;rb&#39;</span><span class="p">),</span> <span class="s">&#39;application/vnd.ms-excel&#39;</span><span class="p">,</span> <span class="p">{</span><span class="s">&#39;Expires&#39;</span><span class="p">:</span> <span class="s">&#39;0&#39;</span><span class="p">})}</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">post</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">files</span><span class="o">=</span><span class="n">files</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">text</span>
<span class="go">{</span>
<span class="go">  ...</span>
<span class="go">  &quot;files&quot;: {</span>
<span class="go">    &quot;file&quot;: &quot;&lt;censored...binary...data&gt;&quot;</span>
<span class="go">  },</span>
<span class="go">  ...</span>
<span class="go">}</span>
</pre></div>
</div>
<p>如果你想，你也可以发送作为文件来接收的字符串:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">url</span> <span class="o">=</span> <span class="s">&#39;http://httpbin.org/post&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">files</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;file&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s">&#39;report.csv&#39;</span><span class="p">,</span> <span class="s">&#39;some,data,to,send</span><span class="se">\n</span><span class="s">another,row,to,send</span><span class="se">\n</span><span class="s">&#39;</span><span class="p">)}</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">post</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">files</span><span class="o">=</span><span class="n">files</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">text</span>
<span class="go">{</span>
<span class="go">  ...</span>
<span class="go">  &quot;files&quot;: {</span>
<span class="go">    &quot;file&quot;: &quot;some,data,to,send\\nanother,row,to,send\\n&quot;</span>
<span class="go">  },</span>
<span class="go">  ...</span>
<span class="go">}</span>
</pre></div>
</div>
<p>如果你发送一个非常大的文件作为 <code class="docutils literal"><span class="pre">multipart/form-data</span></code> 请求，你可能希望流请求(?)。默认下 <code class="docutils literal"><span class="pre">requests</span></code> 不支持, 但有个第三方包支持 -
<code class="docutils literal"><span class="pre">requests-toolbelt</span></code>. 你可以阅读 <a class="reference external" href="https://toolbelt.rtfd.org">toolbelt 文档</a> 来了解使用方法。</p>
<p>在一个请求中发送多文件参考 <a class="reference internal" href="index.html#advanced"><span>高级用法</span></a>
一节.</p>
</div>
<div class="section" id="id7">
<h4>响应状态码<a class="headerlink" href="#id7" title="永久链接至标题">¶</a></h4>
<p>我们可以检测响应状态码:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;http://httpbin.org/get&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">status_code</span>
<span class="go">200</span>
</pre></div>
</div>
<p>为方便引用，Requests还附带了一个内置的状态码查询对象:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">status_code</span> <span class="o">==</span> <span class="n">requests</span><span class="o">.</span><span class="n">codes</span><span class="o">.</span><span class="n">ok</span>
<span class="go">True</span>
</pre></div>
</div>
<p>如果发送了一个失败请求(非200响应)，我们可以通过 <code class="xref py py-class docutils literal"><span class="pre">Response.raise_for_status()</span></code>
来抛出异常:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">bad_r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;http://httpbin.org/status/404&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">bad_r</span><span class="o">.</span><span class="n">status_code</span>
<span class="go">404</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">bad_r</span><span class="o">.</span><span class="n">raise_for_status</span><span class="p">()</span>
<span class="gt">Traceback (most recent call last):</span>
  File <span class="nb">&quot;requests/models.py&quot;</span>, line <span class="m">832</span>, in <span class="n">raise_for_status</span>
    <span class="k">raise</span> <span class="n">http_error</span>
<span class="gr">requests.exceptions.HTTPError</span>: <span class="n">404 Client Error</span>
</pre></div>
</div>
<p>但是，由于我们的例子中 <code class="docutils literal"><span class="pre">r</span></code> 的 <code class="docutils literal"><span class="pre">status_code</span></code> 是 <code class="docutils literal"><span class="pre">200</span></code> ，当我们调用
<code class="docutils literal"><span class="pre">raise_for_status()</span></code> 时，得到的是:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">raise_for_status</span><span class="p">()</span>
<span class="go">None</span>
</pre></div>
</div>
<p>一切都挺和谐哈。</p>
</div>
<div class="section" id="id8">
<h4>响应头<a class="headerlink" href="#id8" title="永久链接至标题">¶</a></h4>
<p>我们可以查看以一个Python字典形式展示的服务器响应头:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">headers</span>
<span class="go">{</span>
<span class="go">    &#39;content-encoding&#39;: &#39;gzip&#39;,</span>
<span class="go">    &#39;transfer-encoding&#39;: &#39;chunked&#39;,</span>
<span class="go">    &#39;connection&#39;: &#39;close&#39;,</span>
<span class="go">    &#39;server&#39;: &#39;nginx/1.0.4&#39;,</span>
<span class="go">    &#39;x-runtime&#39;: &#39;148ms&#39;,</span>
<span class="go">    &#39;etag&#39;: &#39;&quot;e1ca502697e5c9317743dc078f67693f&quot;&#39;,</span>
<span class="go">    &#39;content-type&#39;: &#39;application/json&#39;</span>
<span class="go">}</span>
</pre></div>
</div>
<p>但是这个字典比较特殊：它是仅为HTTP头部而生的。根据 <a class="reference external" href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">RFC 2616</a> ，
HTTP头部是大小写不敏感的。</p>
<p>因此，我们可以使用任意大写形式来访问这些响应头字段:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">headers</span><span class="p">[</span><span class="s">&#39;Content-Type&#39;</span><span class="p">]</span>
<span class="go">&#39;application/json&#39;</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">headers</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;content-type&#39;</span><span class="p">)</span>
<span class="go">&#39;application/json&#39;</span>
</pre></div>
</div>
</div>
<div class="section" id="cookies">
<h4>Cookies<a class="headerlink" href="#cookies" title="永久链接至标题">¶</a></h4>
<p>如果某个响应中包含一些Cookie，你可以快速访问它们:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">url</span> <span class="o">=</span> <span class="s">&#39;http://example.com/some/cookie/setting/url&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">cookies</span><span class="p">[</span><span class="s">&#39;example_cookie_name&#39;</span><span class="p">]</span>
<span class="go">&#39;example_cookie_value&#39;</span>
</pre></div>
</div>
<p>要想发送你的cookies到服务器，可以使用 <code class="docutils literal"><span class="pre">cookies</span></code> 参数:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">url</span> <span class="o">=</span> <span class="s">&#39;http://httpbin.org/cookies&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cookies</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">cookies_are</span><span class="o">=</span><span class="s">&#39;working&#39;</span><span class="p">)</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">cookies</span><span class="o">=</span><span class="n">cookies</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">text</span>
<span class="go">&#39;{&quot;cookies&quot;: {&quot;cookies_are&quot;: &quot;working&quot;}}&#39;</span>
</pre></div>
</div>
</div>
<div class="section" id="id9">
<h4>重定向与请求历史<a class="headerlink" href="#id9" title="永久链接至标题">¶</a></h4>
<p>默认情况下，除了 HEAD, Requests会自动处理所有重定向。</p>
<p>可以使用响应对象的 <code class="docutils literal"><span class="pre">history</span></code> 方法来追踪重定向。</p>
<p><a class="reference internal" href="index.html#requests.Response.history" title="requests.Response.history"><code class="xref py py-class docutils literal"><span class="pre">Response.history</span></code></a> 是一个:class:<cite>Response &lt;requests.Response&gt;</cite> 对象的列表，为了完成请求而创建了这些对象。这个对象列表按照从最老到最近的请求进行排序。</p>
<p>例如，Github将所有的HTTP请求重定向到HTTPS。:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;http://github.com&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">url</span>
<span class="go">&#39;https://github.com/&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">status_code</span>
<span class="go">200</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">history</span>
<span class="go">[&lt;Response [301]&gt;]</span>
</pre></div>
</div>
<p>如果你使用的是GET, OPTIONS, POST, PUT, PATCH 或者 DELETE,，那么你可以通过 <code class="docutils literal"><span class="pre">allow_redirects</span></code> 参数禁用重定向处理:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;http://github.com&#39;</span><span class="p">,</span> <span class="n">allow_redirects</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">status_code</span>
<span class="go">301</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">history</span>
<span class="go">[]</span>
</pre></div>
</div>
<p>如果你使用的是HEAD，你也可以启用重定向:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">head</span><span class="p">(</span><span class="s">&#39;http://github.com&#39;</span><span class="p">,</span> <span class="n">allow_redirects</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">url</span>
<span class="go">&#39;https://github.com/&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">history</span>
<span class="go">[&lt;Response [301]&gt;]</span>
</pre></div>
</div>
</div>
<div class="section" id="id10">
<h4>超时<a class="headerlink" href="#id10" title="永久链接至标题">¶</a></h4>
<p>你可以告诉requests在经过以 <code class="docutils literal"><span class="pre">timeout</span></code> 参数设定的秒数时间之后停止等待响应:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;http://github.com&#39;</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="mf">0.001</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
  File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>
<span class="gr">requests.exceptions.Timeout</span>: <span class="n">HTTPConnectionPool(host=&#39;github.com&#39;, port=80): Request timed out. (timeout=0.001)</span>
</pre></div>
</div>
<div class="admonition- admonition">
<p class="first admonition-title">注:</p>
<p class="last"><code class="docutils literal"><span class="pre">timeout</span></code> 仅对连接过程有效，与响应体的下载无关。
<code class="docutils literal"><span class="pre">timeout</span></code> is not a time limit on the entire response download;
rather, an exception is raised if the server has not issued a
response for <code class="docutils literal"><span class="pre">timeout</span></code> seconds (more precisely, if no bytes have been
received on the underlying socket for <code class="docutils literal"><span class="pre">timeout</span></code> seconds).</p>
</div>
</div>
<div class="section" id="id11">
<h4>错误与异常<a class="headerlink" href="#id11" title="永久链接至标题">¶</a></h4>
<p>遇到网络问题（如：DNS查询失败、拒绝连接等）时，Requests会抛出一个 <code class="xref py py-class docutils literal"><span class="pre">ConnectionError</span></code> 异常。</p>
<p>遇到罕见的无效HTTP响应时，Requests则会抛出一个 <code class="xref py py-class docutils literal"><span class="pre">HTTPError</span></code> 异常。</p>
<p>若请求超时，则抛出一个 <code class="xref py py-class docutils literal"><span class="pre">Timeout</span></code> 异常。</p>
<p>若请求超过了设定的最大重定向次数，则会抛出一个 <code class="xref py py-class docutils literal"><span class="pre">TooManyRedirects</span></code> 异常。</p>
<p>所有Requests显式抛出的异常都继承自 <code class="xref py py-class docutils literal"><span class="pre">requests.exceptions.RequestException</span></code> 。</p>
<hr class="docutils" />
<p>准备好学习更多内容了吗？去 <a class="reference internal" href="index.html#advanced"><span>高级用法</span></a> 一节看看吧。</p>
</div>
</div>
<span id="document-user/advanced"></span><div class="section" id="advanced">
<span id="id1"></span><h3>高级用法<a class="headerlink" href="#advanced" title="永久链接至标题">¶</a></h3>
<p>本篇文档涵盖了Requests的一些更加高级的特性。</p>
<div class="section" id="id2">
<h4>会话对象<a class="headerlink" href="#id2" title="永久链接至标题">¶</a></h4>
<p>会话对象让你能够跨请求保持某些参数。它也会在同一个Session实例发出的所有请求之间保持cookies。</p>
<p>会话对象具有主要的Requests API的所有方法。</p>
<p>我们来跨请求保持一些cookies:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">s</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">Session</span><span class="p">()</span>

<span class="n">s</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;http://httpbin.org/cookies/set/sessioncookie/123456789&#39;</span><span class="p">)</span>
<span class="n">r</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&quot;http://httpbin.org/cookies&quot;</span><span class="p">)</span>

<span class="k">print</span><span class="p">(</span><span class="n">r</span><span class="o">.</span><span class="n">text</span><span class="p">)</span>
<span class="c"># &#39;{&quot;cookies&quot;: {&quot;sessioncookie&quot;: &quot;123456789&quot;}}&#39;</span>
</pre></div>
</div>
<p>会话也可用来为请求方法提供缺省数据。这是通过为会话对象的属性提供数据来实现的:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">s</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">Session</span><span class="p">()</span>
<span class="n">s</span><span class="o">.</span><span class="n">auth</span> <span class="o">=</span> <span class="p">(</span><span class="s">&#39;user&#39;</span><span class="p">,</span> <span class="s">&#39;pass&#39;</span><span class="p">)</span>
<span class="n">s</span><span class="o">.</span><span class="n">headers</span><span class="o">.</span><span class="n">update</span><span class="p">({</span><span class="s">&#39;x-test&#39;</span><span class="p">:</span> <span class="s">&#39;true&#39;</span><span class="p">})</span>

<span class="c"># both &#39;x-test&#39; and &#39;x-test2&#39; are sent</span>
<span class="n">s</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;http://httpbin.org/headers&#39;</span><span class="p">,</span> <span class="n">headers</span><span class="o">=</span><span class="p">{</span><span class="s">&#39;x-test2&#39;</span><span class="p">:</span> <span class="s">&#39;true&#39;</span><span class="p">})</span>
</pre></div>
</div>
<p>任何你传递给请求方法的字典都会与已设置会话层数据合并。方法层的参数覆盖会话的参数。</p>
<div class="admonition- admonition">
<p class="first admonition-title">从字典参数中移除一个值</p>
<p class="last">有时你会想省略字典参数中一些会话层的键。要做到这一点，你只需简单地在方法层参数中将那个键的值设置为 <code class="docutils literal"><span class="pre">None</span></code> ，那个键就会被自动省略掉。</p>
</div>
<p>包含在一个会话中的所有数据你都可以直接使用。学习更多细节请阅读 <a class="reference internal" href="index.html#sessionapi"><span>会话API文档</span></a> 。</p>
</div>
<div class="section" id="id3">
<h4>请求与响应对象<a class="headerlink" href="#id3" title="永久链接至标题">¶</a></h4>
<p>任何时候调用requests.*()你都在做两件主要的事情。其一，你在构建一个 <cite>Request</cite> 对象，
该对象将被发送到某个服务器请求或查询一些资源。其二，一旦 <code class="docutils literal"><span class="pre">requests</span></code> 得到一个从
服务器返回的响应就会产生一个 <code class="docutils literal"><span class="pre">Response</span></code> 对象。该响应对象包含服务器返回的所有信息，
也包含你原来创建的 <code class="docutils literal"><span class="pre">Request</span></code> 对象。如下是一个简单的请求，从Wikipedia的服务器得到
一些非常重要的信息:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;http://en.wikipedia.org/wiki/Monty_Python&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>如果想访问服务器返回给我们的响应头部信息，可以这样做:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">headers</span>
<span class="go">{&#39;content-length&#39;: &#39;56170&#39;, &#39;x-content-type-options&#39;: &#39;nosniff&#39;, &#39;x-cache&#39;:</span>
<span class="go">&#39;HIT from cp1006.eqiad.wmnet, MISS from cp1010.eqiad.wmnet&#39;, &#39;content-encoding&#39;:</span>
<span class="go">&#39;gzip&#39;, &#39;age&#39;: &#39;3080&#39;, &#39;content-language&#39;: &#39;en&#39;, &#39;vary&#39;: &#39;Accept-Encoding,Cookie&#39;,</span>
<span class="go">&#39;server&#39;: &#39;Apache&#39;, &#39;last-modified&#39;: &#39;Wed, 13 Jun 2012 01:33:50 GMT&#39;,</span>
<span class="go">&#39;connection&#39;: &#39;close&#39;, &#39;cache-control&#39;: &#39;private, s-maxage=0, max-age=0,</span>
<span class="go">must-revalidate&#39;, &#39;date&#39;: &#39;Thu, 14 Jun 2012 12:59:39 GMT&#39;, &#39;content-type&#39;:</span>
<span class="go">&#39;text/html; charset=UTF-8&#39;, &#39;x-cache-lookup&#39;: &#39;HIT from cp1006.eqiad.wmnet:3128,</span>
<span class="go">MISS from cp1010.eqiad.wmnet:80&#39;}</span>
</pre></div>
</div>
<p>然而，如果想得到发送到服务器的请求的头部，我们可以简单地访问该请求，然后是该请求的头部:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">headers</span>
<span class="go">{&#39;Accept-Encoding&#39;: &#39;identity, deflate, compress, gzip&#39;,</span>
<span class="go">&#39;Accept&#39;: &#39;*/*&#39;, &#39;User-Agent&#39;: &#39;python-requests/0.13.1&#39;}</span>
</pre></div>
</div>
</div>
<div class="section" id="prepared-requests">
<h4>Prepared Requests<a class="headerlink" href="#prepared-requests" title="永久链接至标题">¶</a></h4>
<p>Whenever you receive a <a class="reference internal" href="index.html#requests.Response" title="requests.Response"><code class="xref py py-class docutils literal"><span class="pre">Response</span></code></a> object
from an API call or a Session call, the <code class="docutils literal"><span class="pre">request</span></code> attribute is actually the
<code class="docutils literal"><span class="pre">PreparedRequest</span></code> that was used. In some cases you may wish to do some extra
work to the body or headers (or anything else really) before sending a
request. The simple recipe for this is the following:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">requests</span> <span class="kn">import</span> <span class="n">Request</span><span class="p">,</span> <span class="n">Session</span>

<span class="n">s</span> <span class="o">=</span> <span class="n">Session</span><span class="p">()</span>
<span class="n">req</span> <span class="o">=</span> <span class="n">Request</span><span class="p">(</span><span class="s">&#39;GET&#39;</span><span class="p">,</span> <span class="n">url</span><span class="p">,</span>
    <span class="n">data</span><span class="o">=</span><span class="n">data</span><span class="p">,</span>
    <span class="n">headers</span><span class="o">=</span><span class="n">header</span>
<span class="p">)</span>
<span class="n">prepped</span> <span class="o">=</span> <span class="n">req</span><span class="o">.</span><span class="n">prepare</span><span class="p">()</span>

<span class="c"># do something with prepped.body</span>
<span class="c"># do something with prepped.headers</span>

<span class="n">resp</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="n">prepped</span><span class="p">,</span>
    <span class="n">stream</span><span class="o">=</span><span class="n">stream</span><span class="p">,</span>
    <span class="n">verify</span><span class="o">=</span><span class="n">verify</span><span class="p">,</span>
    <span class="n">proxies</span><span class="o">=</span><span class="n">proxies</span><span class="p">,</span>
    <span class="n">cert</span><span class="o">=</span><span class="n">cert</span><span class="p">,</span>
    <span class="n">timeout</span><span class="o">=</span><span class="n">timeout</span>
<span class="p">)</span>

<span class="k">print</span><span class="p">(</span><span class="n">resp</span><span class="o">.</span><span class="n">status_code</span><span class="p">)</span>
</pre></div>
</div>
<p>Since you are not doing anything special with the <code class="docutils literal"><span class="pre">Request</span></code> object, you
prepare it immediately and modify the <code class="docutils literal"><span class="pre">PreparedRequest</span></code> object. You then
send that with the other parameters you would have sent to <code class="docutils literal"><span class="pre">requests.*</span></code> or
<code class="docutils literal"><span class="pre">Session.*</span></code>.</p>
<p>However, the above code will lose some of the advantages of having a Requests
<a class="reference internal" href="index.html#requests.Session" title="requests.Session"><code class="xref py py-class docutils literal"><span class="pre">Session</span></code></a> object. In particular,
<a class="reference internal" href="index.html#requests.Session" title="requests.Session"><code class="xref py py-class docutils literal"><span class="pre">Session</span></code></a>-level state such as cookies will
not get applied to your request. To get a
<a class="reference internal" href="index.html#requests.PreparedRequest" title="requests.PreparedRequest"><code class="xref py py-class docutils literal"><span class="pre">PreparedRequest</span></code></a> with that state
applied, replace the call to <a class="reference internal" href="index.html#requests.Request.prepare" title="requests.Request.prepare"><code class="xref py py-meth docutils literal"><span class="pre">Request.prepare()</span></code></a> with a call to
<code class="xref py py-meth docutils literal"><span class="pre">Session.prepare_request()</span></code>, like this:</p>
<div class="highlight-python"><div class="highlight"><pre>from requests import Request, Session

s = Session()
req = Request(&#39;GET&#39;,  url,
    data=data
    headers=headers
)

prepped = s.prepare_request(req)

# do something with prepped.body
# do something with prepped.headers

resp = s.send(prepped,
    stream=stream,
    verify=verify,
    proxies=proxies,
    cert=cert,
    timeout=timeout
)

print(resp.status_code)
</pre></div>
</div>
</div>
<div class="section" id="ssl">
<span id="verification"></span><h4>SSL证书验证<a class="headerlink" href="#ssl" title="永久链接至标题">¶</a></h4>
<p>Requests可以为HTTPS请求验证SSL证书，就像web浏览器一样。要想检查某个主机的SSL证书，你可以使用 <code class="docutils literal"><span class="pre">verify</span></code> 参数:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;https://kennethreitz.com&#39;</span><span class="p">,</span> <span class="n">verify</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="go">requests.exceptions.SSLError: hostname &#39;kennethreitz.com&#39; doesn&#39;t match either of &#39;*.herokuapp.com&#39;, &#39;herokuapp.com&#39;</span>
</pre></div>
</div>
<p>在该域名上我没有设置SSL，所以失败了。但Github设置了SSL:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;https://github.com&#39;</span><span class="p">,</span> <span class="n">verify</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="go">&lt;Response [200]&gt;</span>
</pre></div>
</div>
<p>对于私有证书，你也可以传递一个CA_BUNDLE文件的路径给 <code class="docutils literal"><span class="pre">verify</span></code> 。你也可以设置 <code class="docutils literal"><span class="pre">REQUEST_CA_BUNDLE</span></code> 环境变量。</p>
<p>如果你将 <code class="docutils literal"><span class="pre">verify</span></code> 设置为False，Requests也能忽略对SSL证书的验证。</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;https://kennethreitz.com&#39;</span><span class="p">,</span> <span class="n">verify</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="go">&lt;Response [200]&gt;</span>
</pre></div>
</div>
<p>默认情况下， <code class="docutils literal"><span class="pre">verify</span></code> 是设置为True的。选项 <code class="docutils literal"><span class="pre">verify</span></code> 仅应用于主机证书。</p>
<p>你也可以指定一个本地证书用作客户端证书，可以是单个文件（包含密钥和证书）或一个包含两个文件路径的元组:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;https://kennethreitz.com&#39;</span><span class="p">,</span> <span class="n">cert</span><span class="o">=</span><span class="p">(</span><span class="s">&#39;/path/server.crt&#39;</span><span class="p">,</span> <span class="s">&#39;/path/key&#39;</span><span class="p">))</span>
<span class="go">&lt;Response [200]&gt;</span>
</pre></div>
</div>
<p>如果你指定了一个错误路径或一个无效的证书:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;https://kennethreitz.com&#39;</span><span class="p">,</span> <span class="n">cert</span><span class="o">=</span><span class="s">&#39;/wrong_path/server.pem&#39;</span><span class="p">)</span>
<span class="go">SSLError: [Errno 336265225] _ssl.c:347: error:140B0009:SSL routines:SSL_CTX_use_PrivateKey_file:PEM lib</span>
</pre></div>
</div>
</div>
<div class="section" id="id4">
<h4>响应体内容工作流<a class="headerlink" href="#id4" title="永久链接至标题">¶</a></h4>
<p>默认情况下，当你进行网络请求后，响应体会立即被下载。你可以通过 <code class="docutils literal"><span class="pre">stream</span></code> 参数覆盖这个行为，推迟下载响应体直到访问 <code class="xref py py-class docutils literal"><span class="pre">Response.content</span></code> 属性:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">tarball_url</span> <span class="o">=</span> <span class="s">&#39;https://github.com/kennethreitz/requests/tarball/master&#39;</span>
<span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">tarball_url</span><span class="p">,</span> <span class="n">stream</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</pre></div>
</div>
<p>此时仅有响应头被下载下来了，连接保持打开状态，因此允许我们根据条件获取内容:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">if</span> <span class="nb">int</span><span class="p">(</span><span class="n">r</span><span class="o">.</span><span class="n">headers</span><span class="p">[</span><span class="s">&#39;content-length&#39;</span><span class="p">])</span> <span class="o">&lt;</span> <span class="n">TOO_LONG</span><span class="p">:</span>
  <span class="n">content</span> <span class="o">=</span> <span class="n">r</span><span class="o">.</span><span class="n">content</span>
  <span class="o">...</span>
</pre></div>
</div>
<p>你可以进一步使用 <a class="reference internal" href="index.html#requests.Response.iter_content" title="requests.Response.iter_content"><code class="xref py py-class docutils literal"><span class="pre">Response.iter_content</span></code></a> 和 <a class="reference internal" href="index.html#requests.Response.iter_lines" title="requests.Response.iter_lines"><code class="xref py py-class docutils literal"><span class="pre">Response.iter_lines</span></code></a> 方法来控制工作流，或者以 <a class="reference internal" href="index.html#requests.Response.raw" title="requests.Response.raw"><code class="xref py py-class docutils literal"><span class="pre">Response.raw</span></code></a> 从底层urllib3的 <code class="xref py py-class docutils literal"><span class="pre">urllib3.HTTPResponse</span> <span class="pre">&lt;urllib3.response.HTTPResponse</span></code> 读取。</p>
<p>If you set <code class="docutils literal"><span class="pre">stream</span></code> to <code class="docutils literal"><span class="pre">True</span></code> when making a request, Requests cannot
release the connection back to the pool unless you consume all the data or call
<code class="xref py py-class docutils literal"><span class="pre">Response.close</span></code>. This can lead to
inefficiency with connections. If you find yourself partially reading request
bodies (or not reading them at all) while using <code class="docutils literal"><span class="pre">stream=True</span></code>, you should
consider using <code class="docutils literal"><span class="pre">contextlib.closing</span></code> (<a class="reference external" href="http://docs.python.org/2/library/contextlib.html#contextlib.closing">documented here</a>), like this:</p>
<div class="highlight-python"><div class="highlight"><pre>from contextlib import closing

with closing(requests.get(&#39;http://httpbin.org/get&#39;, stream=True)) as r:
    # Do things with the response here.
</pre></div>
</div>
</div>
<div class="section" id="id5">
<h4>保持活动状态（持久连接）<a class="headerlink" href="#id5" title="永久链接至标题">¶</a></h4>
<p>好消息 - 归功于urllib3，同一会话内的持久连接是完全自动处理的！同一会话内你发出的任何请求都会自动复用恰当的连接！</p>
<p>注意：只有所有的响应体数据被读取完毕连接才会被释放为连接池；所以确保将 <code class="docutils literal"><span class="pre">stream</span></code> 设置为 <code class="docutils literal"><span class="pre">False</span></code> 或读取 <code class="docutils literal"><span class="pre">Response</span></code> 对象的 <code class="docutils literal"><span class="pre">content</span></code> 属性。</p>
</div>
<div class="section" id="id6">
<h4>流式上传<a class="headerlink" href="#id6" title="永久链接至标题">¶</a></h4>
<p>Requests支持流式上传，这允许你发送大的数据流或文件而无需先把它们读入内存。要使用流式上传，仅需为你的请求体提供一个类文件对象即可:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s">&#39;massive-body&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
    <span class="n">requests</span><span class="o">.</span><span class="n">post</span><span class="p">(</span><span class="s">&#39;http://some.url/streamed&#39;</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="n">f</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="id7">
<h4>块编码请求<a class="headerlink" href="#id7" title="永久链接至标题">¶</a></h4>
<p>对于出去和进来的请求，Requests也支持分块传输编码。要发送一个块编码的请求，仅需为你的请求体提供一个生成器（或任意没有具体长度(without a length)的迭代器）:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">gen</span><span class="p">():</span>
    <span class="k">yield</span> <span class="s">&#39;hi&#39;</span>
    <span class="k">yield</span> <span class="s">&#39;there&#39;</span>

<span class="n">requests</span><span class="o">.</span><span class="n">post</span><span class="p">(</span><span class="s">&#39;http://some.url/chunked&#39;</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="n">gen</span><span class="p">())</span>
</pre></div>
</div>
</div>
<div class="section" id="post-multiple-multipart-encoded-files">
<h4>POST Multiple Multipart-Encoded Files<a class="headerlink" href="#post-multiple-multipart-encoded-files" title="永久链接至标题">¶</a></h4>
<p>You can send multiple files in one request. For example, suppose you want to
upload image files to an HTML form with a multiple file field &#8216;images&#8217;:</p>
<blockquote>
<div>&lt;input type=&#8221;file&#8221; name=&#8221;images&#8221; multiple=&#8221;true&#8221; required=&#8221;true&#8221;/&gt;</div></blockquote>
<p>To do that, just set files to a list of tuples of (form_field_name, file_info):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">url</span> <span class="o">=</span> <span class="s">&#39;http://httpbin.org/post&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">multiple_files</span> <span class="o">=</span> <span class="p">[(</span><span class="s">&#39;images&#39;</span><span class="p">,</span> <span class="p">(</span><span class="s">&#39;foo.png&#39;</span><span class="p">,</span> <span class="nb">open</span><span class="p">(</span><span class="s">&#39;foo.png&#39;</span><span class="p">,</span> <span class="s">&#39;rb&#39;</span><span class="p">),</span> <span class="s">&#39;image/png&#39;</span><span class="p">)),</span>
<span class="go">                      (&#39;images&#39;, (&#39;bar.png&#39;, open(&#39;bar.png&#39;, &#39;rb&#39;), &#39;image/png&#39;))]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">post</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">files</span><span class="o">=</span><span class="n">multiple_files</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">text</span>
<span class="go">{</span>
<span class="go">  ...</span>
<span class="go">  &#39;files&#39;: {&#39;images&#39;: &#39; ....&#39;}</span>
<span class="go">  &#39;Content-Type&#39;: &#39;multipart/form-data; boundary=3131623adb2043caaeb5538cc7aa0b3a&#39;,</span>
<span class="go">  ...</span>
<span class="go">}</span>
</pre></div>
</div>
</div>
<div class="section" id="id8">
<h4>事件挂钩<a class="headerlink" href="#id8" title="永久链接至标题">¶</a></h4>
<p>Requests有一个钩子系统，你可以用来操控部分请求过程，或信号事件处理。</p>
<p>可用的钩子:</p>
<p><code class="docutils literal"><span class="pre">response</span></code>:</p>
<blockquote>
<div>从一个请求产生的响应</div></blockquote>
<p>你可以通过传递一个 <code class="docutils literal"><span class="pre">{hook_name:</span> <span class="pre">callback_function}</span></code> 字典给 <code class="docutils literal"><span class="pre">hooks</span></code> 请求参数
为每个请求分配一个钩子函数:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">hooks</span><span class="o">=</span><span class="nb">dict</span><span class="p">(</span><span class="n">response</span><span class="o">=</span><span class="n">print_url</span><span class="p">)</span>
</pre></div>
</div>
<p><code class="docutils literal"><span class="pre">callback_function</span></code> 会接受一个数据块作为它的第一个参数。</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">print_url</span><span class="p">(</span><span class="n">r</span><span class="p">):</span>
    <span class="k">print</span><span class="p">(</span><span class="n">r</span><span class="o">.</span><span class="n">url</span><span class="p">)</span>
</pre></div>
</div>
<p>若执行你的回调函数期间发生错误，系统会给出一个警告。</p>
<p>若回调函数返回一个值，默认以该值替换传进来的数据。若函数未返回任何东西，
也没有什么其他的影响。</p>
<p>我们来在运行期间打印一些请求方法的参数:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;http://httpbin.org&#39;</span><span class="p">,</span> <span class="n">hooks</span><span class="o">=</span><span class="nb">dict</span><span class="p">(</span><span class="n">response</span><span class="o">=</span><span class="n">print_url</span><span class="p">))</span>
<span class="go">http://httpbin.org</span>
<span class="go">&lt;Response [200]&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="id9">
<h4>自定义身份验证<a class="headerlink" href="#id9" title="永久链接至标题">¶</a></h4>
<p>Requests允许你使用自己指定的身份验证机制。</p>
<p>任何传递给请求方法的 <code class="docutils literal"><span class="pre">auth</span></code> 参数的可调用对象，在请求发出之前都有机会修改请求。</p>
<p>自定义的身份验证机制是作为 <code class="docutils literal"><span class="pre">requests.auth.AuthBase</span></code> 的子类来实现的，也非常容易定义。</p>
<p>Requests在 <code class="docutils literal"><span class="pre">requests.auth</span></code> 中提供了两种常见的的身份验证方案： <code class="docutils literal"><span class="pre">HTTPBasicAuth</span></code> 和 <code class="docutils literal"><span class="pre">HTTPDigestAuth</span></code> 。</p>
<p>假设我们有一个web服务，仅在 <code class="docutils literal"><span class="pre">X-Pizza</span></code> 头被设置为一个密码值的情况下才会有响应。虽然这不太可能，
但就以它为例好了</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">requests.auth</span> <span class="kn">import</span> <span class="n">AuthBase</span>

<span class="k">class</span> <span class="nc">PizzaAuth</span><span class="p">(</span><span class="n">AuthBase</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Attaches HTTP Pizza Authentication to the given Request object.&quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">username</span><span class="p">):</span>
        <span class="c"># setup any auth-related data here</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">username</span> <span class="o">=</span> <span class="n">username</span>

    <span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">r</span><span class="p">):</span>
        <span class="c"># modify and return the request</span>
        <span class="n">r</span><span class="o">.</span><span class="n">headers</span><span class="p">[</span><span class="s">&#39;X-Pizza&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">username</span>
        <span class="k">return</span> <span class="n">r</span>
</pre></div>
</div>
<p>然后就可以使用我们的PizzaAuth来进行网络请求:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;http://pizzabin.org/admin&#39;</span><span class="p">,</span> <span class="n">auth</span><span class="o">=</span><span class="n">PizzaAuth</span><span class="p">(</span><span class="s">&#39;kenneth&#39;</span><span class="p">))</span>
<span class="go">&lt;Response [200]&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="streaming-requests">
<span id="id10"></span><h4>流式请求<a class="headerlink" href="#streaming-requests" title="永久链接至标题">¶</a></h4>
<p>使用 <a class="reference internal" href="index.html#requests.Response.iter_lines" title="requests.Response.iter_lines"><code class="xref py py-class docutils literal"><span class="pre">requests.Response.iter_lines()</span></code></a> 你可以很方便地对流式API（例如 <a class="reference external" href="https://dev.twittercom/docs/streaming-api">Twitter的流式API</a> ）进行迭代。简单地设置 <code class="docutils literal"><span class="pre">stream</span></code> 为 <code class="docutils literal"><span class="pre">True</span></code> 便可以使用 <a class="reference internal" href="index.html#requests.Response.iter_lines" title="requests.Response.iter_lines"><code class="xref py py-class docutils literal"><span class="pre">iter_lines()</span></code></a> 对相应进行迭代:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">json</span>
<span class="kn">import</span> <span class="nn">requests</span>

<span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;http://httpbin.org/stream/20&#39;</span><span class="p">,</span> <span class="n">stream</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>

<span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">r</span><span class="o">.</span><span class="n">iter_lines</span><span class="p">():</span>

    <span class="c"># filter out keep-alive new lines</span>
    <span class="k">if</span> <span class="n">line</span><span class="p">:</span>
        <span class="k">print</span><span class="p">(</span><span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">line</span><span class="p">))</span>
</pre></div>
</div>
</div>
<div class="section" id="id11">
<h4>代理<a class="headerlink" href="#id11" title="永久链接至标题">¶</a></h4>
<p>如果需要使用代理，你可以通过为任意请求方法提供 <code class="docutils literal"><span class="pre">proxies</span></code> 参数来配置单个请求:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">requests</span>

<span class="n">proxies</span> <span class="o">=</span> <span class="p">{</span>
  <span class="s">&quot;http&quot;</span><span class="p">:</span> <span class="s">&quot;http://10.10.1.10:3128&quot;</span><span class="p">,</span>
  <span class="s">&quot;https&quot;</span><span class="p">:</span> <span class="s">&quot;http://10.10.1.10:1080&quot;</span><span class="p">,</span>
<span class="p">}</span>

<span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&quot;http://example.org&quot;</span><span class="p">,</span> <span class="n">proxies</span><span class="o">=</span><span class="n">proxies</span><span class="p">)</span>
</pre></div>
</div>
<p>你也可以通过环境变量 <code class="docutils literal"><span class="pre">HTTP_PROXY</span></code> 和 <code class="docutils literal"><span class="pre">HTTPS_PROXY</span></code> 来配置代理。</p>
<div class="highlight-python"><div class="highlight"><pre>$ export HTTP_PROXY=&quot;http://10.10.1.10:3128&quot;
$ export HTTPS_PROXY=&quot;http://10.10.1.10:1080&quot;
$ python
&gt;&gt;&gt; import requests
&gt;&gt;&gt; requests.get(&quot;http://example.org&quot;)
</pre></div>
</div>
<p>若你的代理需要使用HTTP Basic Auth，可以使用 <cite>http://user:password&#64;host/</cite> 语法:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">proxies</span> <span class="o">=</span> <span class="p">{</span>
    <span class="s">&quot;http&quot;</span><span class="p">:</span> <span class="s">&quot;http://user:pass@10.10.1.10:3128/&quot;</span><span class="p">,</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
<div class="section" id="id12">
<h4>合规性<a class="headerlink" href="#id12" title="永久链接至标题">¶</a></h4>
<p>Requests符合所有相关的规范和RFC，这样不会为用户造成不必要的困难。但这种对规范的考虑
导致一些行为对于不熟悉相关规范的人来说看似有点奇怪。</p>
<div class="section" id="id13">
<h5>编码方式<a class="headerlink" href="#id13" title="永久链接至标题">¶</a></h5>
<p>当你收到一个响应时，Requests会猜测响应的编码方式，用于在你调用 <code class="docutils literal"><span class="pre">Response.text</span></code> 方法时
对响应进行解码。Requests首先在HTTP头部检测是否存在指定的编码方式，如果不存在，则会使用
<a class="reference external" href="http://pypi.python.org/pypi/charade">charade</a> 来尝试猜测编码方式。</p>
<p>只有当HTTP头部不存在明确指定的字符集，并且 <code class="docutils literal"><span class="pre">Content-Type</span></code> 头部字段包含 <code class="docutils literal"><span class="pre">text</span></code> 值之时，
Requests才不去猜测编码方式。</p>
<p>在这种情况下，
<a class="reference external" href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7.1">RFC 2616</a> 指定默认字符集
必须是 <code class="docutils literal"><span class="pre">ISO-8859-1</span></code> 。Requests遵从这一规范。如果你需要一种不同的编码方式，你可以手动设置
<code class="docutils literal"><span class="pre">Response.encoding</span></code> 属性，或使用原始的 <code class="docutils literal"><span class="pre">Response.content</span></code> 。</p>
</div>
</div>
<div class="section" id="http">
<h4>HTTP动词<a class="headerlink" href="#http" title="永久链接至标题">¶</a></h4>
<p>Requests提供了几乎所有HTTP动词的功能：GET，OPTIONS， HEAD，POST，PUT，PATCH和DELETE。
以下内容为使用Requests中的这些动词以及Github API提供了详细示例。</p>
<p>我将从最常使用的动词GET开始。HTTP GET是一个幂等的方法，从给定的URL返回一个资源。因而，
当你试图从一个web位置获取数据之时，你应该使用这个动词。一个使用示例是尝试从Github上获取
关于一个特定commit的信息。假设我们想获取Requests的commit <code class="docutils literal"><span class="pre">a050faf</span></code> 的信息。我们可以
这样去做:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">requests</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;https://api.github.com/repos/kennethreitz/requests/git/commits/a050faf084662f3a352dd1a941f2c7c9f886d4ad&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>我们应该确认Github是否正确响应。如果正确响应，我们想弄清响应内容是什么类型的。像这样去做:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">if</span> <span class="p">(</span><span class="n">r</span><span class="o">.</span><span class="n">status_code</span> <span class="o">==</span> <span class="n">requests</span><span class="o">.</span><span class="n">codes</span><span class="o">.</span><span class="n">ok</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">print</span> <span class="n">r</span><span class="o">.</span><span class="n">headers</span><span class="p">[</span><span class="s">&#39;content-type&#39;</span><span class="p">]</span>
<span class="gp">...</span>
<span class="go">application/json; charset=utf-8</span>
</pre></div>
</div>
<p>可见，GitHub返回了JSON数据，非常好，这样就可以使用 <code class="docutils literal"><span class="pre">r.json</span></code> 方法把这个返回的数据解析成Python对象。</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">commit_data</span> <span class="o">=</span> <span class="n">r</span><span class="o">.</span><span class="n">json</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">commit_data</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span>
<span class="go">[u&#39;committer&#39;, u&#39;author&#39;, u&#39;url&#39;, u&#39;tree&#39;, u&#39;sha&#39;, u&#39;parents&#39;, u&#39;message&#39;]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">commit_data</span><span class="p">[</span><span class="s">u&#39;committer&#39;</span><span class="p">]</span>
<span class="go">{u&#39;date&#39;: u&#39;2012-05-10T11:10:50-07:00&#39;, u&#39;email&#39;: u&#39;me@kennethreitz.com&#39;, u&#39;name&#39;: u&#39;Kenneth Reitz&#39;}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">commit_data</span><span class="p">[</span><span class="s">u&#39;message&#39;</span><span class="p">]</span>
<span class="go">makin&#39; history</span>
</pre></div>
</div>
<p>到目前为止，一切都非常简单。嗯，我们来研究一下GitHub的API。我们可以去看看文档，
但如果使用Requests来研究也许会更有意思一点。我们可以借助Requests的OPTIONS动词来看看我们刚使用过的url
支持哪些HTTP方法。</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">verbs</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">options</span><span class="p">(</span><span class="n">r</span><span class="o">.</span><span class="n">url</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">verbs</span><span class="o">.</span><span class="n">status_code</span>
<span class="go">500</span>
</pre></div>
</div>
<p>额，这是怎么回事？毫无帮助嘛！原来GitHub，与许多API提供方一样，实际上并未实现OPTIONS方法。
这是一个恼人的疏忽，但没关系，那我们可以使用枯燥的文档。然而，如果GitHub正确实现了OPTIONS，
那么服务器应该在响应头中返回允许用户使用的HTTP方法，例如</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">verbs</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">options</span><span class="p">(</span><span class="s">&#39;http://a-good-website.com/api/cats&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">verbs</span><span class="o">.</span><span class="n">headers</span><span class="p">[</span><span class="s">&#39;allow&#39;</span><span class="p">]</span>
<span class="go">GET,HEAD,POST,OPTIONS</span>
</pre></div>
</div>
<p>转而去查看文档，我们看到对于提交信息，另一个允许的方法是POST，它会创建一个新的提交。
由于我们正在使用Requests代码库，我们应尽可能避免对它发送笨拙的POST。作为替代，我们来
玩玩GitHub的Issue特性。</p>
<p>本篇文档是回应Issue #482而添加的。鉴于该问题已经存在，我们就以它为例。先获取它。</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;https://api.github.com/repos/kennethreitz/requests/issues/482&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">status_code</span>
<span class="go">200</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">issue</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">r</span><span class="o">.</span><span class="n">text</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">issue</span><span class="p">[</span><span class="s">u&#39;title&#39;</span><span class="p">]</span>
<span class="go">Feature any http verb in docs</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">issue</span><span class="p">[</span><span class="s">u&#39;comments&#39;</span><span class="p">]</span>
<span class="go">3</span>
</pre></div>
</div>
<p>Cool，有3个评论。我们来看一下最后一个评论。</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">r</span><span class="o">.</span><span class="n">url</span> <span class="o">+</span> <span class="s">u&#39;/comments&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">status_code</span>
<span class="go">200</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">comments</span> <span class="o">=</span> <span class="n">r</span><span class="o">.</span><span class="n">json</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">comments</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span>
<span class="go">[u&#39;body&#39;, u&#39;url&#39;, u&#39;created_at&#39;, u&#39;updated_at&#39;, u&#39;user&#39;, u&#39;id&#39;]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">comments</span><span class="p">[</span><span class="mi">2</span><span class="p">][</span><span class="s">u&#39;body&#39;</span><span class="p">]</span>
<span class="go">Probably in the &quot;advanced&quot; section</span>
</pre></div>
</div>
<p>嗯，那看起来似乎是个愚蠢之处。我们发表个评论来告诉这个评论者他自己的愚蠢。那么，这个评论者是谁呢？</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">comments</span><span class="p">[</span><span class="mi">2</span><span class="p">][</span><span class="s">u&#39;user&#39;</span><span class="p">][</span><span class="s">u&#39;login&#39;</span><span class="p">]</span>
<span class="go">kennethreitz</span>
</pre></div>
</div>
<p>好，我们来告诉这个叫肯尼思的家伙，这个例子应该放在快速上手指南中。根据GitHub API文档，
其方法是POST到该话题。我们来试试看。</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">body</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">({</span><span class="s">u&quot;body&quot;</span><span class="p">:</span> <span class="s">u&quot;Sounds great! I&#39;ll get right on it!&quot;</span><span class="p">})</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">url</span> <span class="o">=</span> <span class="s">u&quot;https://api.github.com/repos/kennethreitz/requests/issues/482/comments&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">post</span><span class="p">(</span><span class="n">url</span><span class="o">=</span><span class="n">url</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="n">body</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">status_code</span>
<span class="go">404</span>
</pre></div>
</div>
<p>额，这有点古怪哈。可能我们需要验证身份。那就有点纠结了，对吧？不对。Requests简化了多种身份验证形式的使用，
包括非常常见的Basic Auth。</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">requests.auth</span> <span class="kn">import</span> <span class="n">HTTPBasicAuth</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">auth</span> <span class="o">=</span> <span class="n">HTTPBasicAuth</span><span class="p">(</span><span class="s">&#39;fake@example.com&#39;</span><span class="p">,</span> <span class="s">&#39;not_a_real_password&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">post</span><span class="p">(</span><span class="n">url</span><span class="o">=</span><span class="n">url</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="n">body</span><span class="p">,</span> <span class="n">auth</span><span class="o">=</span><span class="n">auth</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">status_code</span>
<span class="go">201</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">content</span> <span class="o">=</span> <span class="n">r</span><span class="o">.</span><span class="n">json</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span><span class="p">(</span><span class="n">content</span><span class="p">[</span><span class="s">u&#39;body&#39;</span><span class="p">])</span>
<span class="go">Sounds great! I&#39;ll get right on it.</span>
</pre></div>
</div>
<p>精彩！噢，不！我原本是想说等我一会，因为我得去喂一下我的猫。如果我能够编辑这条评论那就好了！
幸运的是，GitHub允许我们使用另一个HTTP动词，PATCH，来编辑评论。我们来试试。</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">print</span><span class="p">(</span><span class="n">content</span><span class="p">[</span><span class="s">u&quot;id&quot;</span><span class="p">])</span>
<span class="go">5804413</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">body</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">({</span><span class="s">u&quot;body&quot;</span><span class="p">:</span> <span class="s">u&quot;Sounds great! I&#39;ll get right on it once I feed my cat.&quot;</span><span class="p">})</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">url</span> <span class="o">=</span> <span class="s">u&quot;https://api.github.com/repos/kennethreitz/requests/issues/comments/5804413&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">patch</span><span class="p">(</span><span class="n">url</span><span class="o">=</span><span class="n">url</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="n">body</span><span class="p">,</span> <span class="n">auth</span><span class="o">=</span><span class="n">auth</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">status_code</span>
<span class="go">200</span>
</pre></div>
</div>
<p>非常好。现在，我们来折磨一下这个叫肯尼思的家伙，我决定要让他急得团团转，也不告诉他是我在捣蛋。
这意味着我想删除这条评论。GitHub允许我们使用完全名副其实的DELETE方法来删除评论。我们来清除该评论。</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">delete</span><span class="p">(</span><span class="n">url</span><span class="o">=</span><span class="n">url</span><span class="p">,</span> <span class="n">auth</span><span class="o">=</span><span class="n">auth</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">status_code</span>
<span class="go">204</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">headers</span><span class="p">[</span><span class="s">&#39;status&#39;</span><span class="p">]</span>
<span class="go">&#39;204 No Content&#39;</span>
</pre></div>
</div>
<p>很好。不见了。最后一件我想知道的事情是我已经使用了多少限额（ratelimit）。查查看，GitHub在响应头部发送这个信息，
因此不必下载整个网页，我将使用一个HEAD请求来获取响应头。</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">head</span><span class="p">(</span><span class="n">url</span><span class="o">=</span><span class="n">url</span><span class="p">,</span> <span class="n">auth</span><span class="o">=</span><span class="n">auth</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">r</span><span class="o">.</span><span class="n">headers</span>
<span class="gp">...</span>
<span class="go">&#39;x-ratelimit-remaining&#39;: &#39;4995&#39;</span>
<span class="go">&#39;x-ratelimit-limit&#39;: &#39;5000&#39;</span>
<span class="gp">...</span>
</pre></div>
</div>
<p>很好。是时候写个Python程序以各种刺激的方式滥用GitHub的API，还可以使用4995次呢。</p>
</div>
<div class="section" id="id14">
<h4>响应头链接字段<a class="headerlink" href="#id14" title="永久链接至标题">¶</a></h4>
<p>许多HTTP API都有响应头链接字段的特性，它们使得API能够更好地自我描述和自我显露。</p>
<p>GitHub在API中为 <a class="reference external" href="http://developer.github.com/v3/#pagination">分页</a> 使用这些特性，例如:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">url</span> <span class="o">=</span> <span class="s">&#39;https://api.github.com/users/kennethreitz/repos?page=1&amp;per_page=10&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">head</span><span class="p">(</span><span class="n">url</span><span class="o">=</span><span class="n">url</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">headers</span><span class="p">[</span><span class="s">&#39;link&#39;</span><span class="p">]</span>
<span class="go">&#39;&lt;https://api.github.com/users/kennethreitz/repos?page=2&amp;per_page=10&gt;; rel=&quot;next&quot;, &lt;https://api.github.com/users/kennethreitz/repos?page=6&amp;per_page=10&gt;; rel=&quot;last&quot;&#39;</span>
</pre></div>
</div>
<p>Requests会自动解析这些响应头链接字段，并使得它们非常易于使用:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">links</span><span class="p">[</span><span class="s">&quot;next&quot;</span><span class="p">]</span>
<span class="go">{&#39;url&#39;: &#39;https://api.github.com/users/kennethreitz/repos?page=2&amp;per_page=10&#39;, &#39;rel&#39;: &#39;next&#39;}</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">links</span><span class="p">[</span><span class="s">&quot;last&quot;</span><span class="p">]</span>
<span class="go">{&#39;url&#39;: &#39;https://api.github.com/users/kennethreitz/repos?page=7&amp;per_page=10&#39;, &#39;rel&#39;: &#39;last&#39;}</span>
</pre></div>
</div>
</div>
<div class="section" id="transport-adapters">
<h4>Transport Adapters<a class="headerlink" href="#transport-adapters" title="永久链接至标题">¶</a></h4>
<p>As of v1.0.0, Requests has moved to a modular internal design. Part of the
reason this was done was to implement Transport Adapters, originally
<a class="reference external" href="http://www.kennethreitz.org/essays/the-future-of-python-http">described here</a>. Transport Adapters provide a mechanism to define interaction
methods for an HTTP service. In particular, they allow you to apply per-service
configuration.</p>
<p>Requests ships with a single Transport Adapter, the <code class="xref py py-class docutils literal"><span class="pre">HTTPAdapter</span></code>. This adapter provides the default Requests
interaction with HTTP and HTTPS using the powerful <a class="reference external" href="https://github.com/shazow/urllib3">urllib3</a> library. Whenever
a Requests <a class="reference internal" href="index.html#requests.Session" title="requests.Session"><code class="xref py py-class docutils literal"><span class="pre">Session</span></code></a> is initialized, one of these is
attached to the <a class="reference internal" href="index.html#requests.Session" title="requests.Session"><code class="xref py py-class docutils literal"><span class="pre">Session</span></code></a> object for HTTP, and one
for HTTPS.</p>
<p>Requests enables users to create and use their own Transport Adapters that
provide specific functionality. Once created, a Transport Adapter can be
mounted to a Session object, along with an indication of which web services
it should apply to.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">Session</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">s</span><span class="o">.</span><span class="n">mount</span><span class="p">(</span><span class="s">&#39;http://www.github.com&#39;</span><span class="p">,</span> <span class="n">MyAdapter</span><span class="p">())</span>
</pre></div>
</div>
<p>The mount call registers a specific instance of a Transport Adapter to a
prefix. Once mounted, any HTTP request made using that session whose URL starts
with the given prefix will use the given Transport Adapter.</p>
<p>Many of the details of implementing a Transport Adapter are beyond the scope of
this documentation, but take a look at the next example for a simple SSL use-
case. For more than that, you might look at subclassing
<code class="docutils literal"><span class="pre">requests.adapters.BaseAdapter</span></code>.</p>
<div class="section" id="example-specific-ssl-version">
<h5>Example: Specific SSL Version<a class="headerlink" href="#example-specific-ssl-version" title="永久链接至标题">¶</a></h5>
<p>The Requests team has made a specific choice to use whatever SSL version is
default in the underlying library (<a class="reference external" href="https://github.com/shazow/urllib3">urllib3</a>). Normally this is fine, but from
time to time, you might find yourself needing to connect to a service-endpoint
that uses a version that isn&#8217;t compatible with the default.</p>
<p>You can use Transport Adapters for this by taking most of the existing
implementation of HTTPAdapter, and adding a parameter <em>ssl_version</em> that gets
passed-through to <cite>urllib3</cite>. We&#8217;ll make a TA that instructs the library to use
SSLv3:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">ssl</span>

<span class="kn">from</span> <span class="nn">requests.adapters</span> <span class="kn">import</span> <span class="n">HTTPAdapter</span>
<span class="kn">from</span> <span class="nn">requests.packages.urllib3.poolmanager</span> <span class="kn">import</span> <span class="n">PoolManager</span>


<span class="k">class</span> <span class="nc">Ssl3HttpAdapter</span><span class="p">(</span><span class="n">HTTPAdapter</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;&quot;Transport adapter&quot; that allows us to use SSLv3.&quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">init_poolmanager</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">connections</span><span class="p">,</span> <span class="n">maxsize</span><span class="p">,</span> <span class="n">block</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">poolmanager</span> <span class="o">=</span> <span class="n">PoolManager</span><span class="p">(</span><span class="n">num_pools</span><span class="o">=</span><span class="n">connections</span><span class="p">,</span>
                                       <span class="n">maxsize</span><span class="o">=</span><span class="n">maxsize</span><span class="p">,</span>
                                       <span class="n">block</span><span class="o">=</span><span class="n">block</span><span class="p">,</span>
                                       <span class="n">ssl_version</span><span class="o">=</span><span class="n">ssl</span><span class="o">.</span><span class="n">PROTOCOL_SSLv3</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="blocking-or-non-blocking">
<h4>Blocking Or Non-Blocking?<a class="headerlink" href="#blocking-or-non-blocking" title="永久链接至标题">¶</a></h4>
<p>With the default Transport Adapter in place, Requests does not provide any kind
of non-blocking IO. The <a class="reference internal" href="index.html#requests.Response.content" title="requests.Response.content"><code class="xref py py-attr docutils literal"><span class="pre">Response.content</span></code></a>
property will block until the entire response has been downloaded. If
you require more granularity, the streaming features of the library (see
<a class="reference internal" href="#streaming-requests"><span>流式请求</span></a>) allow you to retrieve smaller quantities of the
response at a time. However, these calls will still block.</p>
<p>If you are concerned about the use of blocking IO, there are lots of projects
out there that combine Requests with one of Python&#8217;s asynchronicity frameworks.
Two excellent examples are <a class="reference external" href="https://github.com/kennethreitz/grequests">grequests</a> and <a class="reference external" href="https://github.com/ross/requests-futures">requests-futures</a>.</p>
</div>
<div class="section" id="timeouts">
<h4>Timeouts<a class="headerlink" href="#timeouts" title="永久链接至标题">¶</a></h4>
<p>Most requests to external servers should have a timeout attached, in case the
server is not responding in a timely manner. Without a timeout, your code may
hang for minutes or more.</p>
<p>The <strong>connect</strong> timeout is the number of seconds Requests will wait for your
client to establish a connection to a remote machine (corresponding to the
<a class="reference external" href="http://linux.die.net/man/2/connect">connect()</a>) call on the socket. It&#8217;s a good practice to set connect timeouts
to slightly larger than a multiple of 3, which is the default <a class="reference external" href="http://www.hjp.at/doc/rfc/rfc2988.txt">TCP packet
retransmission window</a>.</p>
<p>Once your client has connected to the server and sent the HTTP request, the
<strong>read</strong> timeout is the number of seconds the client will wait for the server
to send a response. (Specifically, it&#8217;s the number of seconds that the client
will wait <em>between</em> bytes sent from the server. In 99.9% of cases, this is the
time before the server sends the first byte).</p>
<p>If you specify a single value for the timeout, like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;https://github.com&#39;</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span>
</pre></div>
</div>
<p>The timeout value will be applied to both the <code class="docutils literal"><span class="pre">connect</span></code> and the <code class="docutils literal"><span class="pre">read</span></code>
timeouts. Specify a tuple if you would like to set the values separately:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;https://github.com&#39;</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="p">(</span><span class="mf">3.05</span><span class="p">,</span> <span class="mi">27</span><span class="p">))</span>
</pre></div>
</div>
<p>If the remote server is very slow, you can tell Requests to wait forever for
a response, by passing None as a timeout value and then retrieving a cup of
coffee.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;https://github.com&#39;</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="ca-certificates">
<h4>CA Certificates<a class="headerlink" href="#ca-certificates" title="永久链接至标题">¶</a></h4>
<p>By default Requests bundles a set of root CAs that it trusts, sourced from the
<a class="reference external" href="https://hg.mozilla.org/mozilla-central/raw-file/tip/security/nss/lib/ckfw/builtins/certdata.txt">Mozilla trust store</a>. However, these are only updated once for each Requests
version. This means that if you pin a Requests version your certificates can
become extremely out of date.</p>
<p>From Requests version 2.4.0 onwards, Requests will attempt to use certificates
from <a class="reference external" href="http://certifi.io/">certifi</a> if it is present on the system. This allows for users to update
their trusted certificates without having to change the code that runs on their
system.</p>
<p>For the sake of security we recommend upgrading certifi frequently!</p>
</div>
</div>
<span id="document-user/authentication"></span><div class="section" id="authentication">
<span id="id1"></span><h3>身份认证<a class="headerlink" href="#authentication" title="永久链接至标题">¶</a></h3>
<p>本篇文档讨论如何配合Requests使用多种身份认证方式。</p>
<p>许多web服务都需要身份认证，并且也有多种不同的认证类型。
以下，我们会从简单到复杂概述Requests中可用的几种身份认证形式。</p>
<div class="section" id="id2">
<h4>基本身份认证<a class="headerlink" href="#id2" title="永久链接至标题">¶</a></h4>
<p>许多要求身份认证的web服务都接受HTTP Basic Auth。这是最简单的一种身份认证，并且Requests
对这种认证方式的支持是直接开箱即可用。</p>
<p>以HTTP Basic Auth发送请求非常简单:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">requests.auth</span> <span class="kn">import</span> <span class="n">HTTPBasicAuth</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;https://api.github.com/user&#39;</span><span class="p">,</span> <span class="n">auth</span><span class="o">=</span><span class="n">HTTPBasicAuth</span><span class="p">(</span><span class="s">&#39;user&#39;</span><span class="p">,</span> <span class="s">&#39;pass&#39;</span><span class="p">))</span>
<span class="go">&lt;Response [200]&gt;</span>
</pre></div>
</div>
<p>事实上，HTTP Basic Auth如此常见，Requests就提供了一种简写的使用方式:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;https://api.github.com/user&#39;</span><span class="p">,</span> <span class="n">auth</span><span class="o">=</span><span class="p">(</span><span class="s">&#39;user&#39;</span><span class="p">,</span> <span class="s">&#39;pass&#39;</span><span class="p">))</span>
<span class="go">&lt;Response [200]&gt;</span>
</pre></div>
</div>
<p>像这样在一个元组中提供认证信息与前一个 <code class="docutils literal"><span class="pre">HTTPBasicAuth</span></code> 例子是完全相同的。</p>
<div class="section" id="netrc-authentication">
<h5>netrc Authentication<a class="headerlink" href="#netrc-authentication" title="永久链接至标题">¶</a></h5>
<p>If no authentication method is given with the <code class="docutils literal"><span class="pre">auth</span></code> argument, Requests will
attempt to get the authentication credentials for the URL&#8217;s hostname from the
user&#8217;s netrc file.</p>
<p>If credentials for the hostname are found, the request is sent with HTTP Basic
Auth.</p>
</div>
</div>
<div class="section" id="id3">
<h4>摘要式身份认证<a class="headerlink" href="#id3" title="永久链接至标题">¶</a></h4>
<p>另一种非常流行的HTTP身份认证形式是摘要式身份认证，Requests对它的支持也是开箱即可用的:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">requests.auth</span> <span class="kn">import</span> <span class="n">HTTPDigestAuth</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">url</span> <span class="o">=</span> <span class="s">&#39;http://httpbin.org/digest-auth/auth/user/pass&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">auth</span><span class="o">=</span><span class="n">HTTPDigestAuth</span><span class="p">(</span><span class="s">&#39;user&#39;</span><span class="p">,</span> <span class="s">&#39;pass&#39;</span><span class="p">))</span>
<span class="go">&lt;Response [200]&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="oauth-1-authentication">
<h4>OAuth 1 Authentication<a class="headerlink" href="#oauth-1-authentication" title="永久链接至标题">¶</a></h4>
<p>A common form of authentication for several web APIs is OAuth. The <code class="docutils literal"><span class="pre">requests-oauthlib</span></code>
library allows Requests users to easily make OAuth authenticated requests:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">requests</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">requests_oauthlib</span> <span class="kn">import</span> <span class="n">OAuth1</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">url</span> <span class="o">=</span> <span class="s">&#39;https://api.twitter.com/1.1/account/verify_credentials.json&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">auth</span> <span class="o">=</span> <span class="n">OAuth1</span><span class="p">(</span><span class="s">&#39;YOUR_APP_KEY&#39;</span><span class="p">,</span> <span class="s">&#39;YOUR_APP_SECRET&#39;</span><span class="p">,</span>
<span class="go">                  &#39;USER_OAUTH_TOKEN&#39;, &#39;USER_OAUTH_TOKEN_SECRET&#39;)</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">auth</span><span class="o">=</span><span class="n">auth</span><span class="p">)</span>
<span class="go">&lt;Response [200]&gt;</span>
</pre></div>
</div>
<p>For more information on how to OAuth flow works, please see the official <a class="reference external" href="http://oauth.net/">OAuth</a> website.
For examples and documentation on requests-oauthlib, please see the <a class="reference external" href="https://github.com/requests/requests-oauthlib">requests_oauthlib</a>
repository on GitHub</p>
</div>
<div class="section" id="id4">
<h4>其他身份认证形式<a class="headerlink" href="#id4" title="永久链接至标题">¶</a></h4>
<p>Requests被设计成允许其他形式的身份认证非常容易快速地插入其中。开源社区的成员
时常为更复杂或不那么常用的身份认证形式编写认证处理插件。其中一些最优秀的已被
收集在 <a class="reference external" href="https://github.com/requests">Requests organization</a> 页面中，包括:</p>
<ul class="simple">
<li><a class="reference external" href="https://github.com/requests/requests-kerberos">Kerberos</a></li>
<li><a class="reference external" href="https://github.com/requests/requests-ntlm">NTLM</a></li>
</ul>
<p>如果你想使用其中任何一种身份认证形式，直接去它们的GitHub页面，依照说明进行。</p>
</div>
<div class="section" id="id5">
<h4>新的身份认证形式<a class="headerlink" href="#id5" title="永久链接至标题">¶</a></h4>
<p>如果你找不到所需要的身份认证形式的一个良好实现，你也可以自己实现它。Requests非常易于添加你自己的身份认证形式。</p>
<p>要想自己实现，就从 <code class="xref py py-class docutils literal"><span class="pre">requests.auth.AuthBase</span></code> 继承一个子类，并实现 <code class="docutils literal"><span class="pre">__call__()</span></code> 方法:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">requests</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">MyAuth</span><span class="p">(</span><span class="n">requests</span><span class="o">.</span><span class="n">auth</span><span class="o">.</span><span class="n">AuthBase</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">r</span><span class="p">):</span>
<span class="gp">... </span>        <span class="c"># Implement my authentication</span>
<span class="gp">... </span>        <span class="k">return</span> <span class="n">r</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">url</span> <span class="o">=</span> <span class="s">&#39;http://httpbin.org/get&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">auth</span><span class="o">=</span><span class="n">MyAuth</span><span class="p">())</span>
<span class="go">&lt;Response [200]&gt;</span>
</pre></div>
</div>
<p>当一个身份认证模块被附加到一个请求上，在设置request期间就会调用该模块。因此 <code class="docutils literal"><span class="pre">__call__</span></code> 方法必须完成使得身份认证生效的所有事情。一些身份认证形式会额外地添加钩子来提供进一步的功能。</p>
<p>可以在 <a class="reference external" href="https://github.com/requests">Requests organization</a> 页面的 <code class="docutils literal"><span class="pre">auth.py</span></code> 文件中找到示例。</p>
</div>
</div>
</div>
</div>
<div class="section" id="id3">
<h2>社区指南<a class="headerlink" href="#id3" title="永久链接至标题">¶</a></h2>
<p>这部分文档主要详细地介绍了Requests的社区支持情况</p>
<div class="toctree-wrapper compound">
<span id="document-community/faq"></span><div class="section" id="faq">
<span id="id1"></span><h3>常见问题<a class="headerlink" href="#faq" title="永久链接至标题">¶</a></h3>
<p>这部分的文档回答了有关requests的常见问题。</p>
<div class="section" id="id2">
<h4>已编码的数据?<a class="headerlink" href="#id2" title="永久链接至标题">¶</a></h4>
<p>Requests 自动解压缩的gzip编码的响应体，并在可能的情况下尽可能的将响应内容解码为unicode.</p>
<p>如果需要的话，你可以直接访问原始响应内容（甚至是套接字）。</p>
</div>
<div class="section" id="user-agents">
<h4>自定义 User-Agents?<a class="headerlink" href="#user-agents" title="永久链接至标题">¶</a></h4>
<p>Requests 允许你使用其它的HTTP Header来轻松的覆盖自带的User-Agent字符串。</p>
</div>
<div class="section" id="httplib2">
<h4>怎么不是Httplib2?<a class="headerlink" href="#httplib2" title="永久链接至标题">¶</a></h4>
<p>Chris Adams 给出来一个很好的总结
<a class="reference external" href="http://news.ycombinator.com/item?id=2884406">Hacker News</a>:</p>
<blockquote>
<div><p>httplib2 is part of why you should use requests: it&#8217;s far more respectable
as a client but not as well documented and it still takes way too much code
for basic operations. I appreciate what httplib2 is trying to do, that
there&#8217;s a ton of hard low-level annoyances in building a modern HTTP
client, but really, just use requests instead. Kenneth Reitz is very
motivated and he gets the degree to which simple things should be simple
whereas httplib2 feels more like an academic exercise than something
people should use to build production systems[1].</p>
<p>Disclosure: I&#8217;m listed in the requests AUTHORS file but can claim credit
for, oh, about 0.0001% of the awesomeness.</p>
<p>1. <a class="reference external" href="http://code.google.com/p/httplib2/issues/detail?id=96">http://code.google.com/p/httplib2/issues/detail?id=96</a> is a good example:
an annoying bug which affect many people, there was a fix available for
months, which worked great when I applied it in a fork and pounded a couple
TB of data through it, but it took over a year to make it into trunk and
even longer to make it onto PyPI where any other project which required &#8221;
httplib2&#8221; would get the working version.</p>
</div></blockquote>
</div>
<div class="section" id="python-3">
<h4>支持 Python 3 吗?<a class="headerlink" href="#python-3" title="永久链接至标题">¶</a></h4>
<p>恭喜！回答是肯定的。下面是官方支持的python平台列表:</p>
<ul class="simple">
<li>Python 2.6</li>
<li>Python 2.7</li>
<li>Python 3.1</li>
<li>Python 3.2</li>
<li>Python 3.3</li>
<li>PyPy 1.9</li>
</ul>
</div>
</div>
<span id="document-community/out-there"></span><div class="section" id="id1">
<h3>整合<a class="headerlink" href="#id1" title="永久链接至标题">¶</a></h3>
<div class="section" id="scraperwiki">
<h4>ScraperWiki<a class="headerlink" href="#scraperwiki" title="永久链接至标题">¶</a></h4>
<p><a class="reference external" href="https://scraperwiki.com/">ScraperWiki</a> is an excellent service that allows
you to run Python, Ruby, and PHP scraper scripts on the web. Now, Requests
v0.6.1 is available to use in your scrapers!</p>
<p>To give it a try, simply:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">requests</span>
</pre></div>
</div>
</div>
<div class="section" id="python-for-ios">
<h4>Python for iOS<a class="headerlink" href="#python-for-ios" title="永久链接至标题">¶</a></h4>
<p>Requests is built into the wonderful <a class="reference external" href="https://itunes.apple.com/us/app/python-2.7-for-ios/id485729872?mt=Python8">Python for iOS</a> runtime!</p>
<p>To give it a try, simply:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">requests</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="articles-talks">
<h3>Articles &amp; Talks<a class="headerlink" href="#articles-talks" title="永久链接至标题">¶</a></h3>
<ul class="simple">
<li><a class="reference external" href="http://gun.io/blog/python-for-the-web/">Python for the Web</a> teaches how to use Python to interact with the web, using Requests.</li>
<li><a class="reference external" href="http://pydanny.blogspot.com/2011/05/python-http-requests-for-humans.html">Daniel Greenfield&#8217;s Review of Requests</a></li>
<li><a class="reference external" href="http://python-for-humans.heroku.com">My &#8216;Python for Humans&#8217; talk</a> ( <a class="reference external" href="http://codeconf.s3.amazonaws.com/2011/pycodeconf/talks/PyCodeConf2011%20-%20Kenneth%20Reitz.m4a">audio</a> )</li>
<li><a class="reference external" href="http://issackelly.github.com/Consuming-Web-APIs-with-Python-Talk/slides/slides.html">Issac Kelly&#8217;s &#8216;Consuming Web APIs&#8217; talk</a></li>
<li><a class="reference external" href="http://arunsag.wordpress.com/2011/08/17/new-package-python-requests-http-for-humans/">Blog post about Requests via Yum</a></li>
<li><a class="reference external" href="http://habrahabr.ru/blogs/python/126262/">Russian blog post introducing Requests</a></li>
<li><a class="reference external" href="http://www.nicosphere.net/requests-urllib2-de-python-simplifie-2432/">French blog post introducing Requests</a></li>
</ul>
</div>
<span id="document-community/support"></span><div class="section" id="support">
<span id="id1"></span><h3>支持<a class="headerlink" href="#support" title="永久链接至标题">¶</a></h3>
<p>对于Requests，如果你有疑问或者是问题的话，下面几种方法能解决：</p>
<div class="section" id="id2">
<h4>发送推文<a class="headerlink" href="#id2" title="永久链接至标题">¶</a></h4>
<p>如果你的问题在140个字符内描述，欢迎在twitter上发送推文至
<a class="reference external" href="http://twitter.com/kennethreitz">&#64;kennethreitz</a>.</p>
</div>
<div class="section" id="id3">
<h4>修复问题<a class="headerlink" href="#id3" title="永久链接至标题">¶</a></h4>
<p>如果你在Requests中注意到了一些意想不到的行为，或者希望看到一个新的功能支持，请查看
<a class="reference external" href="https://github.com/kennethreitz/requests/issues">file an issue on GitHub</a>.</p>
</div>
<div class="section" id="id4">
<h4>邮件<a class="headerlink" href="#id4" title="永久链接至标题">¶</a></h4>
<p>我更乐意回答关于Requests的个人或者更深入的问题。
Feel free to email
<a class="reference external" href="mailto:requests&#37;&#52;&#48;kennethreitz&#46;com">requests<span>&#64;</span>kennethreitz<span>&#46;</span>com</a>.</p>
</div>
<div class="section" id="irc">
<h4>IRC<a class="headerlink" href="#irc" title="永久链接至标题">¶</a></h4>
<p>Requests 的官方Freenode 频道是
<a class="reference external" href="irc://irc.freenode.net/python-requests">#python-requests</a></p>
<p>在 Freenode 上，你也能通过 <strong>kennethreitz</strong> 找到我。</p>
</div>
</div>
<span id="document-community/updates"></span><div class="section" id="updates">
<span id="id1"></span><h3>更新<a class="headerlink" href="#updates" title="永久链接至标题">¶</a></h3>
<p>如果你想和社区以及开发版的 Requests 保持最新的联系，
这有几种方式:</p>
<div class="section" id="github">
<h4>GitHub<a class="headerlink" href="#github" title="永久链接至标题">¶</a></h4>
<p>最好的方式是追踪Requests开发版本的
<a class="reference external" href="https://github.com/kennethreitz/requests">GitHub 库</a>.</p>
</div>
<div class="section" id="twitter">
<h4>Twitter<a class="headerlink" href="#twitter" title="永久链接至标题">¶</a></h4>
<p>我经常推送关于Requests的新功能和新版本.</p>
<p>Follow <a class="reference external" href="https://twitter.com/kennethreitz">&#64;kennethreitz</a> for updates.</p>
</div>
<div class="section" id="id3">
<h4>邮件列表<a class="headerlink" href="#id3" title="永久链接至标题">¶</a></h4>
<p>对于 Requests 的讨论有一个小容量的邮件列表。发送邮件到
<a class="reference external" href="mailto:requests&#37;&#52;&#48;librelist&#46;org">requests<span>&#64;</span>librelist<span>&#46;</span>org</a> 订阅邮件。</p>
</div>
</div>
</div>
</div>
<div class="section" id="api">
<h2>API 文档<a class="headerlink" href="#api" title="永久链接至标题">¶</a></h2>
<p>如果你正在寻找一个特殊函数，类或者方法的话，这部分文档刚好满足你。</p>
<div class="toctree-wrapper compound">
<span id="document-api"></span><div class="section" id="module-requests">
<span id="id1"></span><span id="api"></span><h3>开发者接口<a class="headerlink" href="#module-requests" title="永久链接至标题">¶</a></h3>
<p>这部分文档包含了Requests所有的接口。对于Requests依赖的外部库部分，我们介绍
一些比较重要的并提供规范文档的链接。</p>
<div class="section" id="id2">
<h4>主要接口<a class="headerlink" href="#id2" title="永久链接至标题">¶</a></h4>
<p>Requests所有的功能都可以通过以下7个方法访问。
它们全部都会返回 <a class="reference internal" href="#requests.Response" title="requests.Response"><code class="xref py py-class docutils literal"><span class="pre">Response</span></code></a> 对象的一个实例。</p>
<dl class="function">
<dt id="requests.request">
<code class="descclassname">requests.</code><code class="descname">request</code><span class="sig-paren">(</span><em>method</em>, <em>url</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#requests.request" title="永久链接至目标">¶</a></dt>
<dd><p>Constructs and sends a <a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a>.
Returns <a class="reference internal" href="#requests.Response" title="requests.Response"><code class="xref py py-class docutils literal"><span class="pre">Response</span></code></a> object.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first last simple">
<li><strong>method</strong> &#8211; method for the new <a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a> object.</li>
<li><strong>url</strong> &#8211; URL for the new <a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a> object.</li>
<li><strong>params</strong> &#8211; (optional) Dictionary or bytes to be sent in the query string for the <a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a>.</li>
<li><strong>data</strong> &#8211; (optional) Dictionary, bytes, or file-like object to send in the body of the <a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a>.</li>
<li><strong>headers</strong> &#8211; (optional) Dictionary of HTTP Headers to send with the <a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a>.</li>
<li><strong>cookies</strong> &#8211; (optional) Dict or CookieJar object to send with the <a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a>.</li>
<li><strong>files</strong> &#8211; (optional) Dictionary of &#8216;name&#8217;: file-like-objects (or {&#8216;name&#8217;: (&#8216;filename&#8217;, fileobj)}) for multipart encoding upload.</li>
<li><strong>auth</strong> &#8211; (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth.</li>
<li><strong>timeout</strong> &#8211; (optional) Float describing the timeout of the request.</li>
<li><strong>allow_redirects</strong> &#8211; (optional) Boolean. Set to True if POST/PUT/DELETE redirect following is allowed.</li>
<li><strong>proxies</strong> &#8211; (optional) Dictionary mapping protocol to the URL of the proxy.</li>
<li><strong>verify</strong> &#8211; (optional) if <code class="docutils literal"><span class="pre">True</span></code>, the SSL cert will be verified. A CA_BUNDLE path can also be provided.</li>
<li><strong>stream</strong> &#8211; (optional) if <code class="docutils literal"><span class="pre">False</span></code>, the response content will be immediately downloaded.</li>
<li><strong>cert</strong> &#8211; (optional) if String, path to ssl client cert file (.pem). If Tuple, (&#8216;cert&#8217;, &#8216;key&#8217;) pair.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<p>Usage:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">requests</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">req</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">request</span><span class="p">(</span><span class="s">&#39;GET&#39;</span><span class="p">,</span> <span class="s">&#39;http://httpbin.org/get&#39;</span><span class="p">)</span>
<span class="go">&lt;Response [200]&gt;</span>
</pre></div>
</div>
</dd></dl>

<dl class="function">
<dt id="requests.head">
<code class="descclassname">requests.</code><code class="descname">head</code><span class="sig-paren">(</span><em>url</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#requests.head" title="永久链接至目标">¶</a></dt>
<dd><p>Sends a HEAD request. Returns <a class="reference internal" href="#requests.Response" title="requests.Response"><code class="xref py py-class docutils literal"><span class="pre">Response</span></code></a> object.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first last simple">
<li><strong>url</strong> &#8211; URL for the new <a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a> object.</li>
<li><strong>**kwargs</strong> &#8211; Optional arguments that <code class="docutils literal"><span class="pre">request</span></code> takes.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="function">
<dt id="requests.get">
<code class="descclassname">requests.</code><code class="descname">get</code><span class="sig-paren">(</span><em>url</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#requests.get" title="永久链接至目标">¶</a></dt>
<dd><p>Sends a GET request. Returns <a class="reference internal" href="#requests.Response" title="requests.Response"><code class="xref py py-class docutils literal"><span class="pre">Response</span></code></a> object.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first last simple">
<li><strong>url</strong> &#8211; URL for the new <a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a> object.</li>
<li><strong>**kwargs</strong> &#8211; Optional arguments that <code class="docutils literal"><span class="pre">request</span></code> takes.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="function">
<dt id="requests.post">
<code class="descclassname">requests.</code><code class="descname">post</code><span class="sig-paren">(</span><em>url</em>, <em>data=None</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#requests.post" title="永久链接至目标">¶</a></dt>
<dd><p>Sends a POST request. Returns <a class="reference internal" href="#requests.Response" title="requests.Response"><code class="xref py py-class docutils literal"><span class="pre">Response</span></code></a> object.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first last simple">
<li><strong>url</strong> &#8211; URL for the new <a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a> object.</li>
<li><strong>data</strong> &#8211; (optional) Dictionary, bytes, or file-like object to send in the body of the <a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a>.</li>
<li><strong>**kwargs</strong> &#8211; Optional arguments that <code class="docutils literal"><span class="pre">request</span></code> takes.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="function">
<dt id="requests.put">
<code class="descclassname">requests.</code><code class="descname">put</code><span class="sig-paren">(</span><em>url</em>, <em>data=None</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#requests.put" title="永久链接至目标">¶</a></dt>
<dd><p>Sends a PUT request. Returns <a class="reference internal" href="#requests.Response" title="requests.Response"><code class="xref py py-class docutils literal"><span class="pre">Response</span></code></a> object.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first last simple">
<li><strong>url</strong> &#8211; URL for the new <a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a> object.</li>
<li><strong>data</strong> &#8211; (optional) Dictionary, bytes, or file-like object to send in the body of the <a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a>.</li>
<li><strong>**kwargs</strong> &#8211; Optional arguments that <code class="docutils literal"><span class="pre">request</span></code> takes.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="function">
<dt id="requests.patch">
<code class="descclassname">requests.</code><code class="descname">patch</code><span class="sig-paren">(</span><em>url</em>, <em>data=None</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#requests.patch" title="永久链接至目标">¶</a></dt>
<dd><p>Sends a PATCH request. Returns <a class="reference internal" href="#requests.Response" title="requests.Response"><code class="xref py py-class docutils literal"><span class="pre">Response</span></code></a> object.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first last simple">
<li><strong>url</strong> &#8211; URL for the new <a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a> object.</li>
<li><strong>data</strong> &#8211; (optional) Dictionary, bytes, or file-like object to send in the body of the <a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a>.</li>
<li><strong>**kwargs</strong> &#8211; Optional arguments that <code class="docutils literal"><span class="pre">request</span></code> takes.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="function">
<dt id="requests.delete">
<code class="descclassname">requests.</code><code class="descname">delete</code><span class="sig-paren">(</span><em>url</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#requests.delete" title="永久链接至目标">¶</a></dt>
<dd><p>Sends a DELETE request. Returns <a class="reference internal" href="#requests.Response" title="requests.Response"><code class="xref py py-class docutils literal"><span class="pre">Response</span></code></a> object.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first last simple">
<li><strong>url</strong> &#8211; URL for the new <a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a> object.</li>
<li><strong>**kwargs</strong> &#8211; Optional arguments that <code class="docutils literal"><span class="pre">request</span></code> takes.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<div class="section" id="id3">
<h5>较低级别的类<a class="headerlink" href="#id3" title="永久链接至标题">¶</a></h5>
<dl class="class">
<dt id="requests.Request">
<em class="property">class </em><code class="descclassname">requests.</code><code class="descname">Request</code><span class="sig-paren">(</span><em>method=None</em>, <em>url=None</em>, <em>headers=None</em>, <em>files=None</em>, <em>data={}</em>, <em>params={}</em>, <em>auth=None</em>, <em>cookies=None</em>, <em>hooks=None</em><span class="sig-paren">)</span><a class="headerlink" href="#requests.Request" title="永久链接至目标">¶</a></dt>
<dd><p>A user-created <a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a> object.</p>
<p>Used to prepare a <a class="reference internal" href="#requests.PreparedRequest" title="requests.PreparedRequest"><code class="xref py py-class docutils literal"><span class="pre">PreparedRequest</span></code></a>, which is sent to the server.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first last simple">
<li><strong>method</strong> &#8211; HTTP method to use.</li>
<li><strong>url</strong> &#8211; URL to send.</li>
<li><strong>headers</strong> &#8211; dictionary of headers to send.</li>
<li><strong>files</strong> &#8211; dictionary of {filename: fileobject} files to multipart upload.</li>
<li><strong>data</strong> &#8211; the body to attach the request. If a dictionary is provided, form-encoding will take place.</li>
<li><strong>params</strong> &#8211; dictionary of URL parameters to append to the URL.</li>
<li><strong>auth</strong> &#8211; Auth handler or (user, pass) tuple.</li>
<li><strong>cookies</strong> &#8211; dictionary or CookieJar of cookies to attach to this request.</li>
<li><strong>hooks</strong> &#8211; dictionary of callback hooks, for internal usage.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<p>Usage:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">requests</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">req</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">Request</span><span class="p">(</span><span class="s">&#39;GET&#39;</span><span class="p">,</span> <span class="s">&#39;http://httpbin.org/get&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">req</span><span class="o">.</span><span class="n">prepare</span><span class="p">()</span>
<span class="go">&lt;PreparedRequest [GET]&gt;</span>
</pre></div>
</div>
<dl class="method">
<dt id="requests.Request.deregister_hook">
<code class="descname">deregister_hook</code><span class="sig-paren">(</span><em>event</em>, <em>hook</em><span class="sig-paren">)</span><a class="headerlink" href="#requests.Request.deregister_hook" title="永久链接至目标">¶</a></dt>
<dd><p>Deregister a previously registered hook.
Returns True if the hook existed, False if not.</p>
</dd></dl>

<dl class="method">
<dt id="requests.Request.prepare">
<code class="descname">prepare</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#requests.Request.prepare" title="永久链接至目标">¶</a></dt>
<dd><p>Constructs a <a class="reference internal" href="#requests.PreparedRequest" title="requests.PreparedRequest"><code class="xref py py-class docutils literal"><span class="pre">PreparedRequest</span></code></a> for transmission and returns it.</p>
</dd></dl>

<dl class="method">
<dt id="requests.Request.register_hook">
<code class="descname">register_hook</code><span class="sig-paren">(</span><em>event</em>, <em>hook</em><span class="sig-paren">)</span><a class="headerlink" href="#requests.Request.register_hook" title="永久链接至目标">¶</a></dt>
<dd><p>Properly register a hook.</p>
</dd></dl>

</dd></dl>

<dl class="class">
<dt id="requests.Response">
<em class="property">class </em><code class="descclassname">requests.</code><code class="descname">Response</code><a class="headerlink" href="#requests.Response" title="永久链接至目标">¶</a></dt>
<dd><p>The <a class="reference internal" href="#requests.Response" title="requests.Response"><code class="xref py py-class docutils literal"><span class="pre">Response</span></code></a> object, which contains a
server&#8217;s response to an HTTP request.</p>
<dl class="attribute">
<dt id="requests.Response.apparent_encoding">
<code class="descname">apparent_encoding</code><a class="headerlink" href="#requests.Response.apparent_encoding" title="永久链接至目标">¶</a></dt>
<dd><p>The apparent encoding, provided by the lovely Charade library
(Thanks, Ian!).</p>
</dd></dl>

<dl class="attribute">
<dt id="requests.Response.content">
<code class="descname">content</code><a class="headerlink" href="#requests.Response.content" title="永久链接至目标">¶</a></dt>
<dd><p>Content of the response, in bytes.</p>
</dd></dl>

<dl class="attribute">
<dt id="requests.Response.cookies">
<code class="descname">cookies</code><em class="property"> = None</em><a class="headerlink" href="#requests.Response.cookies" title="永久链接至目标">¶</a></dt>
<dd><p>A CookieJar of Cookies the server sent back.</p>
</dd></dl>

<dl class="attribute">
<dt id="requests.Response.elapsed">
<code class="descname">elapsed</code><em class="property"> = None</em><a class="headerlink" href="#requests.Response.elapsed" title="永久链接至目标">¶</a></dt>
<dd><p>The amount of time elapsed between sending the request
and the arrival of the response (as a timedelta)</p>
</dd></dl>

<dl class="attribute">
<dt id="requests.Response.encoding">
<code class="descname">encoding</code><em class="property"> = None</em><a class="headerlink" href="#requests.Response.encoding" title="永久链接至目标">¶</a></dt>
<dd><p>Encoding to decode with when accessing r.text.</p>
</dd></dl>

<dl class="attribute">
<dt id="requests.Response.headers">
<code class="descname">headers</code><em class="property"> = None</em><a class="headerlink" href="#requests.Response.headers" title="永久链接至目标">¶</a></dt>
<dd><p>Case-insensitive Dictionary of Response Headers.
For example, <code class="docutils literal"><span class="pre">headers['content-encoding']</span></code> will return the
value of a <code class="docutils literal"><span class="pre">'Content-Encoding'</span></code> response header.</p>
</dd></dl>

<dl class="attribute">
<dt id="requests.Response.history">
<code class="descname">history</code><em class="property"> = None</em><a class="headerlink" href="#requests.Response.history" title="永久链接至目标">¶</a></dt>
<dd><p>A list of <a class="reference internal" href="#requests.Response" title="requests.Response"><code class="xref py py-class docutils literal"><span class="pre">Response</span></code></a> objects from
the history of the Request. Any redirect responses will end
up here. The list is sorted from the oldest to the most recent request.</p>
</dd></dl>

<dl class="method">
<dt id="requests.Response.iter_content">
<code class="descname">iter_content</code><span class="sig-paren">(</span><em>chunk_size=1</em>, <em>decode_unicode=False</em><span class="sig-paren">)</span><a class="headerlink" href="#requests.Response.iter_content" title="永久链接至目标">¶</a></dt>
<dd><p>Iterates over the response data.  This avoids reading the content
at once into memory for large responses.  The chunk size is the number
of bytes it should read into memory.  This is not necessarily the
length of each item returned as decoding can take place.</p>
</dd></dl>

<dl class="method">
<dt id="requests.Response.iter_lines">
<code class="descname">iter_lines</code><span class="sig-paren">(</span><em>chunk_size=512</em>, <em>decode_unicode=None</em><span class="sig-paren">)</span><a class="headerlink" href="#requests.Response.iter_lines" title="永久链接至目标">¶</a></dt>
<dd><p>Iterates over the response data, one line at a time.  This
avoids reading the content at once into memory for large
responses.</p>
</dd></dl>

<dl class="method">
<dt id="requests.Response.json">
<code class="descname">json</code><span class="sig-paren">(</span><em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#requests.Response.json" title="永久链接至目标">¶</a></dt>
<dd><p>Returns the json-encoded content of a response, if any.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><strong>**kwargs</strong> &#8211; Optional arguments that <code class="docutils literal"><span class="pre">json.loads</span></code> takes.</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="attribute">
<dt id="requests.Response.links">
<code class="descname">links</code><a class="headerlink" href="#requests.Response.links" title="永久链接至目标">¶</a></dt>
<dd><p>Returns the parsed header links of the response, if any.</p>
</dd></dl>

<dl class="method">
<dt id="requests.Response.raise_for_status">
<code class="descname">raise_for_status</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#requests.Response.raise_for_status" title="永久链接至目标">¶</a></dt>
<dd><p>Raises stored <a class="reference internal" href="#requests.HTTPError" title="requests.HTTPError"><code class="xref py py-class docutils literal"><span class="pre">HTTPError</span></code></a>, if one occurred.</p>
</dd></dl>

<dl class="attribute">
<dt id="requests.Response.raw">
<code class="descname">raw</code><em class="property"> = None</em><a class="headerlink" href="#requests.Response.raw" title="永久链接至目标">¶</a></dt>
<dd><p>File-like object representation of response (for advanced usage).
Requires that <a href="#id4"><span class="problematic" id="id5">``</span></a>stream=True` on the request.</p>
</dd></dl>

<dl class="attribute">
<dt id="requests.Response.status_code">
<code class="descname">status_code</code><em class="property"> = None</em><a class="headerlink" href="#requests.Response.status_code" title="永久链接至目标">¶</a></dt>
<dd><p>Integer Code of responded HTTP Status.</p>
</dd></dl>

<dl class="attribute">
<dt id="requests.Response.text">
<code class="descname">text</code><a class="headerlink" href="#requests.Response.text" title="永久链接至目标">¶</a></dt>
<dd><p>Content of the response, in unicode.</p>
<p>if Response.encoding is None and chardet module is available, encoding
will be guessed.</p>
</dd></dl>

<dl class="attribute">
<dt id="requests.Response.url">
<code class="descname">url</code><em class="property"> = None</em><a class="headerlink" href="#requests.Response.url" title="永久链接至目标">¶</a></dt>
<dd><p>Final URL location of Response.</p>
</dd></dl>

</dd></dl>

</div>
</div>
<div class="section" id="id6">
<h4>请求会话<a class="headerlink" href="#id6" title="永久链接至标题">¶</a></h4>
<dl class="class">
<dt id="requests.Session">
<em class="property">class </em><code class="descclassname">requests.</code><code class="descname">Session</code><a class="headerlink" href="#requests.Session" title="永久链接至目标">¶</a></dt>
<dd><p>A Requests session.</p>
<p>Provides cookie persistience, connection-pooling, and configuration.</p>
<p>Basic Usage:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">requests</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">Session</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">s</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;http://httpbin.org/get&#39;</span><span class="p">)</span>
<span class="go">200</span>
</pre></div>
</div>
<dl class="attribute">
<dt id="requests.Session.auth">
<code class="descname">auth</code><em class="property"> = None</em><a class="headerlink" href="#requests.Session.auth" title="永久链接至目标">¶</a></dt>
<dd><p>Default Authentication tuple or object to attach to
<a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a>.</p>
</dd></dl>

<dl class="attribute">
<dt id="requests.Session.cert">
<code class="descname">cert</code><em class="property"> = None</em><a class="headerlink" href="#requests.Session.cert" title="永久链接至目标">¶</a></dt>
<dd><p>SSL certificate default.</p>
</dd></dl>

<dl class="method">
<dt id="requests.Session.close">
<code class="descname">close</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#requests.Session.close" title="永久链接至目标">¶</a></dt>
<dd><p>Closes all adapters and as such the session</p>
</dd></dl>

<dl class="method">
<dt id="requests.Session.delete">
<code class="descname">delete</code><span class="sig-paren">(</span><em>url</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#requests.Session.delete" title="永久链接至目标">¶</a></dt>
<dd><p>Sends a DELETE request. Returns <a class="reference internal" href="#requests.Response" title="requests.Response"><code class="xref py py-class docutils literal"><span class="pre">Response</span></code></a> object.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first last simple">
<li><strong>url</strong> &#8211; URL for the new <a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a> object.</li>
<li><strong>**kwargs</strong> &#8211; Optional arguments that <code class="docutils literal"><span class="pre">request</span></code> takes.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="method">
<dt id="requests.Session.get">
<code class="descname">get</code><span class="sig-paren">(</span><em>url</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#requests.Session.get" title="永久链接至目标">¶</a></dt>
<dd><p>Sends a GET request. Returns <a class="reference internal" href="#requests.Response" title="requests.Response"><code class="xref py py-class docutils literal"><span class="pre">Response</span></code></a> object.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first last simple">
<li><strong>url</strong> &#8211; URL for the new <a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a> object.</li>
<li><strong>**kwargs</strong> &#8211; Optional arguments that <code class="docutils literal"><span class="pre">request</span></code> takes.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="method">
<dt id="requests.Session.get_adapter">
<code class="descname">get_adapter</code><span class="sig-paren">(</span><em>url</em><span class="sig-paren">)</span><a class="headerlink" href="#requests.Session.get_adapter" title="永久链接至目标">¶</a></dt>
<dd><p>Returns the appropriate connnection adapter for the given URL.</p>
</dd></dl>

<dl class="method">
<dt id="requests.Session.head">
<code class="descname">head</code><span class="sig-paren">(</span><em>url</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#requests.Session.head" title="永久链接至目标">¶</a></dt>
<dd><p>Sends a HEAD request. Returns <a class="reference internal" href="#requests.Response" title="requests.Response"><code class="xref py py-class docutils literal"><span class="pre">Response</span></code></a> object.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first last simple">
<li><strong>url</strong> &#8211; URL for the new <a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a> object.</li>
<li><strong>**kwargs</strong> &#8211; Optional arguments that <code class="docutils literal"><span class="pre">request</span></code> takes.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="attribute">
<dt id="requests.Session.headers">
<code class="descname">headers</code><em class="property"> = None</em><a class="headerlink" href="#requests.Session.headers" title="永久链接至目标">¶</a></dt>
<dd><p>A case-insensitive dictionary of headers to be sent on each
<a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a> sent from this
<a class="reference internal" href="#requests.Session" title="requests.Session"><code class="xref py py-class docutils literal"><span class="pre">Session</span></code></a>.</p>
</dd></dl>

<dl class="attribute">
<dt id="requests.Session.hooks">
<code class="descname">hooks</code><em class="property"> = None</em><a class="headerlink" href="#requests.Session.hooks" title="永久链接至目标">¶</a></dt>
<dd><p>Event-handling hooks.</p>
</dd></dl>

<dl class="attribute">
<dt id="requests.Session.max_redirects">
<code class="descname">max_redirects</code><em class="property"> = None</em><a class="headerlink" href="#requests.Session.max_redirects" title="永久链接至目标">¶</a></dt>
<dd><p>Maximum number of redirects to follow.</p>
</dd></dl>

<dl class="method">
<dt id="requests.Session.mount">
<code class="descname">mount</code><span class="sig-paren">(</span><em>prefix</em>, <em>adapter</em><span class="sig-paren">)</span><a class="headerlink" href="#requests.Session.mount" title="永久链接至目标">¶</a></dt>
<dd><p>Registers a connection adapter to a prefix.</p>
</dd></dl>

<dl class="method">
<dt id="requests.Session.options">
<code class="descname">options</code><span class="sig-paren">(</span><em>url</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#requests.Session.options" title="永久链接至目标">¶</a></dt>
<dd><p>Sends a OPTIONS request. Returns <a class="reference internal" href="#requests.Response" title="requests.Response"><code class="xref py py-class docutils literal"><span class="pre">Response</span></code></a> object.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first last simple">
<li><strong>url</strong> &#8211; URL for the new <a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a> object.</li>
<li><strong>**kwargs</strong> &#8211; Optional arguments that <code class="docutils literal"><span class="pre">request</span></code> takes.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="attribute">
<dt id="requests.Session.params">
<code class="descname">params</code><em class="property"> = None</em><a class="headerlink" href="#requests.Session.params" title="永久链接至目标">¶</a></dt>
<dd><p>Dictionary of querystring data to attach to each
<a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a>. The dictionary values may be lists for
representing multivalued query parameters.</p>
</dd></dl>

<dl class="method">
<dt id="requests.Session.patch">
<code class="descname">patch</code><span class="sig-paren">(</span><em>url</em>, <em>data=None</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#requests.Session.patch" title="永久链接至目标">¶</a></dt>
<dd><p>Sends a PATCH request. Returns <a class="reference internal" href="#requests.Response" title="requests.Response"><code class="xref py py-class docutils literal"><span class="pre">Response</span></code></a> object.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first last simple">
<li><strong>url</strong> &#8211; URL for the new <a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a> object.</li>
<li><strong>data</strong> &#8211; (optional) Dictionary, bytes, or file-like object to send in the body of the <a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a>.</li>
<li><strong>**kwargs</strong> &#8211; Optional arguments that <code class="docutils literal"><span class="pre">request</span></code> takes.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="method">
<dt id="requests.Session.post">
<code class="descname">post</code><span class="sig-paren">(</span><em>url</em>, <em>data=None</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#requests.Session.post" title="永久链接至目标">¶</a></dt>
<dd><p>Sends a POST request. Returns <a class="reference internal" href="#requests.Response" title="requests.Response"><code class="xref py py-class docutils literal"><span class="pre">Response</span></code></a> object.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first last simple">
<li><strong>url</strong> &#8211; URL for the new <a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a> object.</li>
<li><strong>data</strong> &#8211; (optional) Dictionary, bytes, or file-like object to send in the body of the <a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a>.</li>
<li><strong>**kwargs</strong> &#8211; Optional arguments that <code class="docutils literal"><span class="pre">request</span></code> takes.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="attribute">
<dt id="requests.Session.proxies">
<code class="descname">proxies</code><em class="property"> = None</em><a class="headerlink" href="#requests.Session.proxies" title="永久链接至目标">¶</a></dt>
<dd><p>Dictionary mapping protocol to the URL of the proxy (e.g.
{&#8216;http&#8217;: &#8216;foo.bar:3128&#8217;}) to be used on each
<a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a>.</p>
</dd></dl>

<dl class="method">
<dt id="requests.Session.put">
<code class="descname">put</code><span class="sig-paren">(</span><em>url</em>, <em>data=None</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#requests.Session.put" title="永久链接至目标">¶</a></dt>
<dd><p>Sends a PUT request. Returns <a class="reference internal" href="#requests.Response" title="requests.Response"><code class="xref py py-class docutils literal"><span class="pre">Response</span></code></a> object.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first last simple">
<li><strong>url</strong> &#8211; URL for the new <a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a> object.</li>
<li><strong>data</strong> &#8211; (optional) Dictionary, bytes, or file-like object to send in the body of the <a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a>.</li>
<li><strong>**kwargs</strong> &#8211; Optional arguments that <code class="docutils literal"><span class="pre">request</span></code> takes.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="method">
<dt id="requests.Session.resolve_redirects">
<code class="descname">resolve_redirects</code><span class="sig-paren">(</span><em>resp</em>, <em>req</em>, <em>stream=False</em>, <em>timeout=None</em>, <em>verify=True</em>, <em>cert=None</em>, <em>proxies=None</em><span class="sig-paren">)</span><a class="headerlink" href="#requests.Session.resolve_redirects" title="永久链接至目标">¶</a></dt>
<dd><p>Receives a Response. Returns a generator of Responses.</p>
</dd></dl>

<dl class="method">
<dt id="requests.Session.send">
<code class="descname">send</code><span class="sig-paren">(</span><em>request</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#requests.Session.send" title="永久链接至目标">¶</a></dt>
<dd><p>Send a given PreparedRequest.</p>
</dd></dl>

<dl class="attribute">
<dt id="requests.Session.stream">
<code class="descname">stream</code><em class="property"> = None</em><a class="headerlink" href="#requests.Session.stream" title="永久链接至目标">¶</a></dt>
<dd><p>Stream response content default.</p>
</dd></dl>

<dl class="attribute">
<dt id="requests.Session.trust_env">
<code class="descname">trust_env</code><em class="property"> = None</em><a class="headerlink" href="#requests.Session.trust_env" title="永久链接至目标">¶</a></dt>
<dd><p>Should we trust the environment?</p>
</dd></dl>

<dl class="attribute">
<dt id="requests.Session.verify">
<code class="descname">verify</code><em class="property"> = None</em><a class="headerlink" href="#requests.Session.verify" title="永久链接至目标">¶</a></dt>
<dd><p>SSL Verification default.</p>
</dd></dl>

</dd></dl>

<div class="section" id="module-requests">
<span id="id7"></span><h5>异常<a class="headerlink" href="#module-requests" title="永久链接至标题">¶</a></h5>
<dl class="exception">
<dt id="requests.RequestException">
<em class="property">exception </em><code class="descclassname">requests.</code><code class="descname">RequestException</code><a class="headerlink" href="#requests.RequestException" title="永久链接至目标">¶</a></dt>
<dd><p>There was an ambiguous exception that occurred while handling your
request.</p>
</dd></dl>

<dl class="exception">
<dt id="requests.ConnectionError">
<em class="property">exception </em><code class="descclassname">requests.</code><code class="descname">ConnectionError</code><a class="headerlink" href="#requests.ConnectionError" title="永久链接至目标">¶</a></dt>
<dd><p>A Connection error occurred.</p>
</dd></dl>

<dl class="exception">
<dt id="requests.HTTPError">
<em class="property">exception </em><code class="descclassname">requests.</code><code class="descname">HTTPError</code><span class="sig-paren">(</span><em>*args</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#requests.HTTPError" title="永久链接至目标">¶</a></dt>
<dd><p>An HTTP error occurred.</p>
</dd></dl>

<dl class="exception">
<dt id="requests.URLRequired">
<em class="property">exception </em><code class="descclassname">requests.</code><code class="descname">URLRequired</code><a class="headerlink" href="#requests.URLRequired" title="永久链接至目标">¶</a></dt>
<dd><p>A valid URL is required to make a request.</p>
</dd></dl>

<dl class="exception">
<dt id="requests.TooManyRedirects">
<em class="property">exception </em><code class="descclassname">requests.</code><code class="descname">TooManyRedirects</code><a class="headerlink" href="#requests.TooManyRedirects" title="永久链接至目标">¶</a></dt>
<dd><p>Too many redirects.</p>
</dd></dl>

</div>
<div class="section" id="id8">
<h5>状态码查询<a class="headerlink" href="#id8" title="永久链接至标题">¶</a></h5>
<dl class="function">
<dt id="requests.codes">
<code class="descclassname">requests.</code><code class="descname">codes</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#requests.codes" title="永久链接至目标">¶</a></dt>
<dd><p>Dictionary lookup object.</p>
</dd></dl>

<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">requests</span><span class="o">.</span><span class="n">codes</span><span class="p">[</span><span class="s">&#39;temporary_redirect&#39;</span><span class="p">]</span>
<span class="go">307</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">requests</span><span class="o">.</span><span class="n">codes</span><span class="o">.</span><span class="n">teapot</span>
<span class="go">418</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">requests</span><span class="o">.</span><span class="n">codes</span><span class="p">[</span><span class="s">&#39;\o/&#39;</span><span class="p">]</span>
<span class="go">200</span>
</pre></div>
</div>
</div>
<div class="section" id="cookies">
<h5>Cookies<a class="headerlink" href="#cookies" title="永久链接至标题">¶</a></h5>
</div>
<div class="section" id="id9">
<h5>编码<a class="headerlink" href="#id9" title="永久链接至标题">¶</a></h5>
</div>
<div class="section" id="id10">
<h5>类<a class="headerlink" href="#id10" title="永久链接至标题">¶</a></h5>
<dl class="class">
<dt>
<em class="property">class </em><code class="descclassname">requests.</code><code class="descname">Response</code></dt>
<dd><p>The <a class="reference internal" href="#requests.Response" title="requests.Response"><code class="xref py py-class docutils literal"><span class="pre">Response</span></code></a> object, which contains a
server&#8217;s response to an HTTP request.</p>
<dl class="attribute">
<dt>
<code class="descname">apparent_encoding</code></dt>
<dd><p>The apparent encoding, provided by the lovely Charade library
(Thanks, Ian!).</p>
</dd></dl>

<dl class="attribute">
<dt>
<code class="descname">content</code></dt>
<dd><p>Content of the response, in bytes.</p>
</dd></dl>

<dl class="attribute">
<dt>
<code class="descname">cookies</code><em class="property"> = None</em></dt>
<dd><p>A CookieJar of Cookies the server sent back.</p>
</dd></dl>

<dl class="attribute">
<dt>
<code class="descname">elapsed</code><em class="property"> = None</em></dt>
<dd><p>The amount of time elapsed between sending the request
and the arrival of the response (as a timedelta)</p>
</dd></dl>

<dl class="attribute">
<dt>
<code class="descname">encoding</code><em class="property"> = None</em></dt>
<dd><p>Encoding to decode with when accessing r.text.</p>
</dd></dl>

<dl class="attribute">
<dt>
<code class="descname">headers</code><em class="property"> = None</em></dt>
<dd><p>Case-insensitive Dictionary of Response Headers.
For example, <code class="docutils literal"><span class="pre">headers['content-encoding']</span></code> will return the
value of a <code class="docutils literal"><span class="pre">'Content-Encoding'</span></code> response header.</p>
</dd></dl>

<dl class="attribute">
<dt>
<code class="descname">history</code><em class="property"> = None</em></dt>
<dd><p>A list of <a class="reference internal" href="#requests.Response" title="requests.Response"><code class="xref py py-class docutils literal"><span class="pre">Response</span></code></a> objects from
the history of the Request. Any redirect responses will end
up here. The list is sorted from the oldest to the most recent request.</p>
</dd></dl>

<dl class="method">
<dt>
<code class="descname">iter_content</code><span class="sig-paren">(</span><em>chunk_size=1</em>, <em>decode_unicode=False</em><span class="sig-paren">)</span></dt>
<dd><p>Iterates over the response data.  This avoids reading the content
at once into memory for large responses.  The chunk size is the number
of bytes it should read into memory.  This is not necessarily the
length of each item returned as decoding can take place.</p>
</dd></dl>

<dl class="method">
<dt>
<code class="descname">iter_lines</code><span class="sig-paren">(</span><em>chunk_size=512</em>, <em>decode_unicode=None</em><span class="sig-paren">)</span></dt>
<dd><p>Iterates over the response data, one line at a time.  This
avoids reading the content at once into memory for large
responses.</p>
</dd></dl>

<dl class="method">
<dt>
<code class="descname">json</code><span class="sig-paren">(</span><em>**kwargs</em><span class="sig-paren">)</span></dt>
<dd><p>Returns the json-encoded content of a response, if any.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><strong>**kwargs</strong> &#8211; Optional arguments that <code class="docutils literal"><span class="pre">json.loads</span></code> takes.</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="attribute">
<dt>
<code class="descname">links</code></dt>
<dd><p>Returns the parsed header links of the response, if any.</p>
</dd></dl>

<dl class="method">
<dt>
<code class="descname">raise_for_status</code><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
<dd><p>Raises stored <a class="reference internal" href="#requests.HTTPError" title="requests.HTTPError"><code class="xref py py-class docutils literal"><span class="pre">HTTPError</span></code></a>, if one occurred.</p>
</dd></dl>

<dl class="attribute">
<dt>
<code class="descname">raw</code><em class="property"> = None</em></dt>
<dd><p>File-like object representation of response (for advanced usage).
Requires that <a href="#id11"><span class="problematic" id="id12">``</span></a>stream=True` on the request.</p>
</dd></dl>

<dl class="attribute">
<dt>
<code class="descname">status_code</code><em class="property"> = None</em></dt>
<dd><p>Integer Code of responded HTTP Status.</p>
</dd></dl>

<dl class="attribute">
<dt>
<code class="descname">text</code></dt>
<dd><p>Content of the response, in unicode.</p>
<p>if Response.encoding is None and chardet module is available, encoding
will be guessed.</p>
</dd></dl>

<dl class="attribute">
<dt>
<code class="descname">url</code><em class="property"> = None</em></dt>
<dd><p>Final URL location of Response.</p>
</dd></dl>

</dd></dl>

<dl class="class">
<dt>
<em class="property">class </em><code class="descclassname">requests.</code><code class="descname">Request</code><span class="sig-paren">(</span><em>method=None</em>, <em>url=None</em>, <em>headers=None</em>, <em>files=None</em>, <em>data={}</em>, <em>params={}</em>, <em>auth=None</em>, <em>cookies=None</em>, <em>hooks=None</em><span class="sig-paren">)</span></dt>
<dd><p>A user-created <a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a> object.</p>
<p>Used to prepare a <a class="reference internal" href="#requests.PreparedRequest" title="requests.PreparedRequest"><code class="xref py py-class docutils literal"><span class="pre">PreparedRequest</span></code></a>, which is sent to the server.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first last simple">
<li><strong>method</strong> &#8211; HTTP method to use.</li>
<li><strong>url</strong> &#8211; URL to send.</li>
<li><strong>headers</strong> &#8211; dictionary of headers to send.</li>
<li><strong>files</strong> &#8211; dictionary of {filename: fileobject} files to multipart upload.</li>
<li><strong>data</strong> &#8211; the body to attach the request. If a dictionary is provided, form-encoding will take place.</li>
<li><strong>params</strong> &#8211; dictionary of URL parameters to append to the URL.</li>
<li><strong>auth</strong> &#8211; Auth handler or (user, pass) tuple.</li>
<li><strong>cookies</strong> &#8211; dictionary or CookieJar of cookies to attach to this request.</li>
<li><strong>hooks</strong> &#8211; dictionary of callback hooks, for internal usage.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<p>Usage:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">requests</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">req</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">Request</span><span class="p">(</span><span class="s">&#39;GET&#39;</span><span class="p">,</span> <span class="s">&#39;http://httpbin.org/get&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">req</span><span class="o">.</span><span class="n">prepare</span><span class="p">()</span>
<span class="go">&lt;PreparedRequest [GET]&gt;</span>
</pre></div>
</div>
<dl class="method">
<dt>
<code class="descname">deregister_hook</code><span class="sig-paren">(</span><em>event</em>, <em>hook</em><span class="sig-paren">)</span></dt>
<dd><p>Deregister a previously registered hook.
Returns True if the hook existed, False if not.</p>
</dd></dl>

<dl class="method">
<dt>
<code class="descname">prepare</code><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
<dd><p>Constructs a <a class="reference internal" href="#requests.PreparedRequest" title="requests.PreparedRequest"><code class="xref py py-class docutils literal"><span class="pre">PreparedRequest</span></code></a> for transmission and returns it.</p>
</dd></dl>

<dl class="method">
<dt>
<code class="descname">register_hook</code><span class="sig-paren">(</span><em>event</em>, <em>hook</em><span class="sig-paren">)</span></dt>
<dd><p>Properly register a hook.</p>
</dd></dl>

</dd></dl>

<dl class="class">
<dt id="requests.PreparedRequest">
<em class="property">class </em><code class="descclassname">requests.</code><code class="descname">PreparedRequest</code><a class="headerlink" href="#requests.PreparedRequest" title="永久链接至目标">¶</a></dt>
<dd><p>The fully mutable <a class="reference internal" href="#requests.PreparedRequest" title="requests.PreparedRequest"><code class="xref py py-class docutils literal"><span class="pre">PreparedRequest</span></code></a> object,
containing the exact bytes that will be sent to the server.</p>
<p>Generated from either a <a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a> object or manually.</p>
<p>Usage:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">requests</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">req</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">Request</span><span class="p">(</span><span class="s">&#39;GET&#39;</span><span class="p">,</span> <span class="s">&#39;http://httpbin.org/get&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">req</span><span class="o">.</span><span class="n">prepare</span><span class="p">()</span>
<span class="go">&lt;PreparedRequest [GET]&gt;</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">Session</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">s</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="n">r</span><span class="p">)</span>
<span class="go">&lt;Response [200]&gt;</span>
</pre></div>
</div>
<dl class="attribute">
<dt id="requests.PreparedRequest.body">
<code class="descname">body</code><em class="property"> = None</em><a class="headerlink" href="#requests.PreparedRequest.body" title="永久链接至目标">¶</a></dt>
<dd><p>request body to send to the server.</p>
</dd></dl>

<dl class="method">
<dt id="requests.PreparedRequest.deregister_hook">
<code class="descname">deregister_hook</code><span class="sig-paren">(</span><em>event</em>, <em>hook</em><span class="sig-paren">)</span><a class="headerlink" href="#requests.PreparedRequest.deregister_hook" title="永久链接至目标">¶</a></dt>
<dd><p>Deregister a previously registered hook.
Returns True if the hook existed, False if not.</p>
</dd></dl>

<dl class="attribute">
<dt id="requests.PreparedRequest.headers">
<code class="descname">headers</code><em class="property"> = None</em><a class="headerlink" href="#requests.PreparedRequest.headers" title="永久链接至目标">¶</a></dt>
<dd><p>dictionary of HTTP headers.</p>
</dd></dl>

<dl class="attribute">
<dt id="requests.PreparedRequest.hooks">
<code class="descname">hooks</code><em class="property"> = None</em><a class="headerlink" href="#requests.PreparedRequest.hooks" title="永久链接至目标">¶</a></dt>
<dd><p>dictionary of callback hooks, for internal usage.</p>
</dd></dl>

<dl class="attribute">
<dt id="requests.PreparedRequest.method">
<code class="descname">method</code><em class="property"> = None</em><a class="headerlink" href="#requests.PreparedRequest.method" title="永久链接至目标">¶</a></dt>
<dd><p>HTTP verb to send to the server.</p>
</dd></dl>

<dl class="attribute">
<dt id="requests.PreparedRequest.path_url">
<code class="descname">path_url</code><a class="headerlink" href="#requests.PreparedRequest.path_url" title="永久链接至目标">¶</a></dt>
<dd><p>Build the path URL to use.</p>
</dd></dl>

<dl class="method">
<dt id="requests.PreparedRequest.prepare_auth">
<code class="descname">prepare_auth</code><span class="sig-paren">(</span><em>auth</em><span class="sig-paren">)</span><a class="headerlink" href="#requests.PreparedRequest.prepare_auth" title="永久链接至目标">¶</a></dt>
<dd><p>Prepares the given HTTP auth data.</p>
</dd></dl>

<dl class="method">
<dt id="requests.PreparedRequest.prepare_body">
<code class="descname">prepare_body</code><span class="sig-paren">(</span><em>data</em>, <em>files</em><span class="sig-paren">)</span><a class="headerlink" href="#requests.PreparedRequest.prepare_body" title="永久链接至目标">¶</a></dt>
<dd><p>Prepares the given HTTP body data.</p>
</dd></dl>

<dl class="method">
<dt id="requests.PreparedRequest.prepare_cookies">
<code class="descname">prepare_cookies</code><span class="sig-paren">(</span><em>cookies</em><span class="sig-paren">)</span><a class="headerlink" href="#requests.PreparedRequest.prepare_cookies" title="永久链接至目标">¶</a></dt>
<dd><p>Prepares the given HTTP cookie data.</p>
</dd></dl>

<dl class="method">
<dt id="requests.PreparedRequest.prepare_headers">
<code class="descname">prepare_headers</code><span class="sig-paren">(</span><em>headers</em><span class="sig-paren">)</span><a class="headerlink" href="#requests.PreparedRequest.prepare_headers" title="永久链接至目标">¶</a></dt>
<dd><p>Prepares the given HTTP headers.</p>
</dd></dl>

<dl class="method">
<dt id="requests.PreparedRequest.prepare_hooks">
<code class="descname">prepare_hooks</code><span class="sig-paren">(</span><em>hooks</em><span class="sig-paren">)</span><a class="headerlink" href="#requests.PreparedRequest.prepare_hooks" title="永久链接至目标">¶</a></dt>
<dd><p>Prepares the given hooks.</p>
</dd></dl>

<dl class="method">
<dt id="requests.PreparedRequest.prepare_method">
<code class="descname">prepare_method</code><span class="sig-paren">(</span><em>method</em><span class="sig-paren">)</span><a class="headerlink" href="#requests.PreparedRequest.prepare_method" title="永久链接至目标">¶</a></dt>
<dd><p>Prepares the given HTTP method.</p>
</dd></dl>

<dl class="method">
<dt id="requests.PreparedRequest.prepare_url">
<code class="descname">prepare_url</code><span class="sig-paren">(</span><em>url</em>, <em>params</em><span class="sig-paren">)</span><a class="headerlink" href="#requests.PreparedRequest.prepare_url" title="永久链接至目标">¶</a></dt>
<dd><p>Prepares the given HTTP URL.</p>
</dd></dl>

<dl class="method">
<dt id="requests.PreparedRequest.register_hook">
<code class="descname">register_hook</code><span class="sig-paren">(</span><em>event</em>, <em>hook</em><span class="sig-paren">)</span><a class="headerlink" href="#requests.PreparedRequest.register_hook" title="永久链接至目标">¶</a></dt>
<dd><p>Properly register a hook.</p>
</dd></dl>

<dl class="attribute">
<dt id="requests.PreparedRequest.url">
<code class="descname">url</code><em class="property"> = None</em><a class="headerlink" href="#requests.PreparedRequest.url" title="永久链接至目标">¶</a></dt>
<dd><p>HTTP URL to send the request to.</p>
</dd></dl>

</dd></dl>

<span class="target" id="sessionapi"></span><dl class="class">
<dt>
<em class="property">class </em><code class="descclassname">requests.</code><code class="descname">Session</code></dt>
<dd><p>A Requests session.</p>
<p>Provides cookie persistience, connection-pooling, and configuration.</p>
<p>Basic Usage:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">requests</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">Session</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">s</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;http://httpbin.org/get&#39;</span><span class="p">)</span>
<span class="go">200</span>
</pre></div>
</div>
<dl class="attribute">
<dt>
<code class="descname">auth</code><em class="property"> = None</em></dt>
<dd><p>Default Authentication tuple or object to attach to
<a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a>.</p>
</dd></dl>

<dl class="attribute">
<dt>
<code class="descname">cert</code><em class="property"> = None</em></dt>
<dd><p>SSL certificate default.</p>
</dd></dl>

<dl class="method">
<dt>
<code class="descname">close</code><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
<dd><p>Closes all adapters and as such the session</p>
</dd></dl>

<dl class="method">
<dt>
<code class="descname">delete</code><span class="sig-paren">(</span><em>url</em>, <em>**kwargs</em><span class="sig-paren">)</span></dt>
<dd><p>Sends a DELETE request. Returns <a class="reference internal" href="#requests.Response" title="requests.Response"><code class="xref py py-class docutils literal"><span class="pre">Response</span></code></a> object.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first last simple">
<li><strong>url</strong> &#8211; URL for the new <a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a> object.</li>
<li><strong>**kwargs</strong> &#8211; Optional arguments that <code class="docutils literal"><span class="pre">request</span></code> takes.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="method">
<dt>
<code class="descname">get</code><span class="sig-paren">(</span><em>url</em>, <em>**kwargs</em><span class="sig-paren">)</span></dt>
<dd><p>Sends a GET request. Returns <a class="reference internal" href="#requests.Response" title="requests.Response"><code class="xref py py-class docutils literal"><span class="pre">Response</span></code></a> object.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first last simple">
<li><strong>url</strong> &#8211; URL for the new <a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a> object.</li>
<li><strong>**kwargs</strong> &#8211; Optional arguments that <code class="docutils literal"><span class="pre">request</span></code> takes.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="method">
<dt>
<code class="descname">get_adapter</code><span class="sig-paren">(</span><em>url</em><span class="sig-paren">)</span></dt>
<dd><p>Returns the appropriate connnection adapter for the given URL.</p>
</dd></dl>

<dl class="method">
<dt>
<code class="descname">head</code><span class="sig-paren">(</span><em>url</em>, <em>**kwargs</em><span class="sig-paren">)</span></dt>
<dd><p>Sends a HEAD request. Returns <a class="reference internal" href="#requests.Response" title="requests.Response"><code class="xref py py-class docutils literal"><span class="pre">Response</span></code></a> object.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first last simple">
<li><strong>url</strong> &#8211; URL for the new <a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a> object.</li>
<li><strong>**kwargs</strong> &#8211; Optional arguments that <code class="docutils literal"><span class="pre">request</span></code> takes.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="attribute">
<dt>
<code class="descname">headers</code><em class="property"> = None</em></dt>
<dd><p>A case-insensitive dictionary of headers to be sent on each
<a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a> sent from this
<a class="reference internal" href="#requests.Session" title="requests.Session"><code class="xref py py-class docutils literal"><span class="pre">Session</span></code></a>.</p>
</dd></dl>

<dl class="attribute">
<dt>
<code class="descname">hooks</code><em class="property"> = None</em></dt>
<dd><p>Event-handling hooks.</p>
</dd></dl>

<dl class="attribute">
<dt>
<code class="descname">max_redirects</code><em class="property"> = None</em></dt>
<dd><p>Maximum number of redirects to follow.</p>
</dd></dl>

<dl class="method">
<dt>
<code class="descname">mount</code><span class="sig-paren">(</span><em>prefix</em>, <em>adapter</em><span class="sig-paren">)</span></dt>
<dd><p>Registers a connection adapter to a prefix.</p>
</dd></dl>

<dl class="method">
<dt>
<code class="descname">options</code><span class="sig-paren">(</span><em>url</em>, <em>**kwargs</em><span class="sig-paren">)</span></dt>
<dd><p>Sends a OPTIONS request. Returns <a class="reference internal" href="#requests.Response" title="requests.Response"><code class="xref py py-class docutils literal"><span class="pre">Response</span></code></a> object.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first last simple">
<li><strong>url</strong> &#8211; URL for the new <a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a> object.</li>
<li><strong>**kwargs</strong> &#8211; Optional arguments that <code class="docutils literal"><span class="pre">request</span></code> takes.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="attribute">
<dt>
<code class="descname">params</code><em class="property"> = None</em></dt>
<dd><p>Dictionary of querystring data to attach to each
<a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a>. The dictionary values may be lists for
representing multivalued query parameters.</p>
</dd></dl>

<dl class="method">
<dt>
<code class="descname">patch</code><span class="sig-paren">(</span><em>url</em>, <em>data=None</em>, <em>**kwargs</em><span class="sig-paren">)</span></dt>
<dd><p>Sends a PATCH request. Returns <a class="reference internal" href="#requests.Response" title="requests.Response"><code class="xref py py-class docutils literal"><span class="pre">Response</span></code></a> object.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first last simple">
<li><strong>url</strong> &#8211; URL for the new <a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a> object.</li>
<li><strong>data</strong> &#8211; (optional) Dictionary, bytes, or file-like object to send in the body of the <a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a>.</li>
<li><strong>**kwargs</strong> &#8211; Optional arguments that <code class="docutils literal"><span class="pre">request</span></code> takes.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="method">
<dt>
<code class="descname">post</code><span class="sig-paren">(</span><em>url</em>, <em>data=None</em>, <em>**kwargs</em><span class="sig-paren">)</span></dt>
<dd><p>Sends a POST request. Returns <a class="reference internal" href="#requests.Response" title="requests.Response"><code class="xref py py-class docutils literal"><span class="pre">Response</span></code></a> object.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first last simple">
<li><strong>url</strong> &#8211; URL for the new <a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a> object.</li>
<li><strong>data</strong> &#8211; (optional) Dictionary, bytes, or file-like object to send in the body of the <a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a>.</li>
<li><strong>**kwargs</strong> &#8211; Optional arguments that <code class="docutils literal"><span class="pre">request</span></code> takes.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="attribute">
<dt>
<code class="descname">proxies</code><em class="property"> = None</em></dt>
<dd><p>Dictionary mapping protocol to the URL of the proxy (e.g.
{&#8216;http&#8217;: &#8216;foo.bar:3128&#8217;}) to be used on each
<a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a>.</p>
</dd></dl>

<dl class="method">
<dt>
<code class="descname">put</code><span class="sig-paren">(</span><em>url</em>, <em>data=None</em>, <em>**kwargs</em><span class="sig-paren">)</span></dt>
<dd><p>Sends a PUT request. Returns <a class="reference internal" href="#requests.Response" title="requests.Response"><code class="xref py py-class docutils literal"><span class="pre">Response</span></code></a> object.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first last simple">
<li><strong>url</strong> &#8211; URL for the new <a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a> object.</li>
<li><strong>data</strong> &#8211; (optional) Dictionary, bytes, or file-like object to send in the body of the <a class="reference internal" href="#requests.Request" title="requests.Request"><code class="xref py py-class docutils literal"><span class="pre">Request</span></code></a>.</li>
<li><strong>**kwargs</strong> &#8211; Optional arguments that <code class="docutils literal"><span class="pre">request</span></code> takes.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="method">
<dt>
<code class="descname">resolve_redirects</code><span class="sig-paren">(</span><em>resp</em>, <em>req</em>, <em>stream=False</em>, <em>timeout=None</em>, <em>verify=True</em>, <em>cert=None</em>, <em>proxies=None</em><span class="sig-paren">)</span></dt>
<dd><p>Receives a Response. Returns a generator of Responses.</p>
</dd></dl>

<dl class="method">
<dt>
<code class="descname">send</code><span class="sig-paren">(</span><em>request</em>, <em>**kwargs</em><span class="sig-paren">)</span></dt>
<dd><p>Send a given PreparedRequest.</p>
</dd></dl>

<dl class="attribute">
<dt>
<code class="descname">stream</code><em class="property"> = None</em></dt>
<dd><p>Stream response content default.</p>
</dd></dl>

<dl class="attribute">
<dt>
<code class="descname">trust_env</code><em class="property"> = None</em></dt>
<dd><p>Should we trust the environment?</p>
</dd></dl>

<dl class="attribute">
<dt>
<code class="descname">verify</code><em class="property"> = None</em></dt>
<dd><p>SSL Verification default.</p>
</dd></dl>

</dd></dl>

</div>
</div>
<div class="section" id="x">
<h4>迁移到 1.x<a class="headerlink" href="#x" title="永久链接至标题">¶</a></h4>
<p>本节详细介绍0.X和1.x的主要区别，减少升级带来的一些不便。</p>
<div class="section" id="id13">
<h5>API 改变<a class="headerlink" href="#id13" title="永久链接至标题">¶</a></h5>
<ul>
<li><p class="first"><code class="docutils literal"><span class="pre">Response.json</span></code> 现在是可调用的并且不再是响应体的属性。</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">requests</span>
<span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;https://github.com/timeline.json&#39;</span><span class="p">)</span>
<span class="n">r</span><span class="o">.</span><span class="n">json</span><span class="p">()</span>   <span class="c"># This *call* raises an exception if JSON decoding fails</span>
</pre></div>
</div>
</li>
<li><p class="first"><code class="docutils literal"><span class="pre">Session</span></code> API 也发生了变化. Sessions 对象不在需要参数了。
<code class="docutils literal"><span class="pre">Session</span></code> is also now capitalized,但为了向后兼容，它仍然能被小写的 <code class="docutils literal"><span class="pre">session</span></code> 实例化。</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">s</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">Session</span><span class="p">()</span>    <span class="c"># formerly, session took parameters</span>
<span class="n">s</span><span class="o">.</span><span class="n">auth</span> <span class="o">=</span> <span class="n">auth</span>
<span class="n">s</span><span class="o">.</span><span class="n">headers</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">headers</span><span class="p">)</span>
<span class="n">r</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;http://httpbin.org/headers&#39;</span><span class="p">)</span>
</pre></div>
</div>
</li>
<li><p class="first">除了&#8217;response&#8217;，所有的请求挂钩已被移除。</p>
</li>
<li><p class="first">认证助手已经被分解成单独的模块了. 参见
<a class="reference external" href="https://github.com/requests/requests-oauthlib">requests-oauthlib</a> and <a class="reference external" href="https://github.com/requests/requests-kerberos">requests-kerberos</a>.</p>
</li>
</ul>
<ul>
<li><p class="first">流请求的参数已从 <code class="docutils literal"><span class="pre">prefetch</span></code> 改变到
<code class="docutils literal"><span class="pre">stream</span></code> ，并且逻辑也被颠倒。除此之外, <code class="docutils literal"><span class="pre">stream</span></code> 现在对于原始响应读取也是必需的。</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># in 0.x, passing prefetch=False would accomplish the same thing</span>
<span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;https://github.com/timeline.json&#39;</span><span class="p">,</span> <span class="n">stream</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="n">r</span><span class="o">.</span><span class="n">raw</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
</pre></div>
</div>
</li>
<li><p class="first">requests 方法的``config`` 参数已全部删除。 现在配置这些选项都在 <code class="docutils literal"><span class="pre">Session</span></code> ，比如 keep-alive 和最大数目的重定向。 详细程度选项应当由配置日志来处理。</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># Verbosity should now be configured with logging</span>
<span class="n">my_config</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;verbose&#39;</span><span class="p">:</span> <span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="p">}</span>
<span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;http://httpbin.org/headers&#39;</span><span class="p">,</span> <span class="n">config</span><span class="o">=</span><span class="n">my_config</span><span class="p">)</span>  <span class="c"># bad!</span>
</pre></div>
</div>
</li>
</ul>
</div>
<div class="section" id="id14">
<h5>许可<a class="headerlink" href="#id14" title="永久链接至标题">¶</a></h5>
<p>有一个关键的与 API 无关的区别是开放许可从 <a class="reference external" href="http://opensource.org/licenses/ISC">ISC</a> 许可 变更到 <a class="reference external" href="http://opensource.org/licenses/Apache-2.0">Apache 2.0</a> 许可.  Apache 2.0
license 确保了对于requests的贡献也被涵盖在 Apache
2.0 许可内.</p>
</div>
</div>
</div>
</div>
</div>
<div class="section" id="id4">
<h2>贡献者指南<a class="headerlink" href="#id4" title="永久链接至标题">¶</a></h2>
<p>如果你对这个项目有兴趣，想做一点贡献，请参考下面文档</p>
<div class="toctree-wrapper compound">
<span id="document-dev/philosophy"></span><div class="section" id="id1">
<h3>开发理念<a class="headerlink" href="#id1" title="永久链接至标题">¶</a></h3>
<p>Requests is an open but opinionated library, created by an open but opinionated developer.</p>
<div class="section" id="benevolent-dictator">
<h4>Benevolent Dictator<a class="headerlink" href="#benevolent-dictator" title="永久链接至标题">¶</a></h4>
<p><a class="reference external" href="http://kennethreitz.org">Kenneth Reitz</a> is the BDFL. He has final say in any decision related to Requests.</p>
</div>
<div class="section" id="values">
<h4>Values<a class="headerlink" href="#values" title="永久链接至标题">¶</a></h4>
<ul class="simple">
<li>Simplicity is always better than functionality.</li>
<li>Listen to everyone, then disregard it.</li>
<li>The API is all that matters. Everything else is secondary.</li>
<li>Fit the 90% use-case. Ignore the nay-sayers.</li>
</ul>
</div>
<div class="section" id="semantic-versioning">
<h4>Semantic Versioning<a class="headerlink" href="#semantic-versioning" title="永久链接至标题">¶</a></h4>
<p>For many years, the open source community has been plagued with version number dystonia. Numbers vary so greatly from project to project, they are practically meaningless.</p>
<p>Requests uses <a class="reference external" href="http://semver.org">Semantic Versioning</a>. This specification seeks to put an end to this madness with a small set of practical guidelines for you and your colleagues to use in your next project.</p>
</div>
<div class="section" id="standard-library">
<h4>Standard Library?<a class="headerlink" href="#standard-library" title="永久链接至标题">¶</a></h4>
<p>Requests has no <em>active</em> plans to be included in the standard library. This decision has been discussed at length with Guido as well as numerous core developers.</p>
<p>Essentially, the standard library is where a library goes to die. It is appropriate for a module to be included when active development is no longer necessary.</p>
<p>Requests just reached v1.0.0. This huge milestone marks a major step in the right direction.</p>
</div>
<div class="section" id="linux-distro-packages">
<h4>Linux Distro Packages<a class="headerlink" href="#linux-distro-packages" title="永久链接至标题">¶</a></h4>
<p>Distributions have been made for many Linux repositories, including: Ubuntu, Debian, RHEL, and Arch.</p>
<p>These distributions are sometimes divergent forks, or are otherwise not kept up-to-date with the latest code and bugfixes. PyPI (and its mirrors) and GitHub are the official distribution sources; alternatives are not supported by the requests project.</p>
</div>
</div>
<span id="document-dev/todo"></span><div class="section" id="id1">
<h3>如何帮助<a class="headerlink" href="#id1" title="永久链接至标题">¶</a></h3>
<p>Requests is under active development, and contributions are more than welcome!</p>
<ol class="arabic simple">
<li>Check for open issues or open a fresh issue to start a discussion around a bug.
There is a Contributor Friendly tag for issues that should be ideal for people who are not very
familiar with the codebase yet.</li>
<li>Fork <a class="reference external" href="https://github.com/kennethreitz/requests">the repository</a> on Github andstart making your
changes to a new branch.</li>
<li>Write a test which shows that the bug was fixed.</li>
<li>Send a pull request and bug the maintainer until it gets merged and published. :)
Make sure to add yourself to <a class="reference external" href="https://github.com/kennethreitz/requests/blob/master/AUTHORS.rst">AUTHORS</a>.</li>
</ol>
<div class="section" id="feature-freeze">
<h4>Feature Freeze<a class="headerlink" href="#feature-freeze" title="永久链接至标题">¶</a></h4>
<p>As of v1.0.0, Requests has now entered a feature freeze. Requests for new
features and Pull Requests implementing those features will not be accepted.</p>
</div>
<div class="section" id="development-dependencies">
<h4>Development Dependencies<a class="headerlink" href="#development-dependencies" title="永久链接至标题">¶</a></h4>
<p>You&#8217;ll need to install py.test in order to run the Requests&#8217; test suite:</p>
<div class="highlight-python"><div class="highlight"><pre>$ make test-deps
$ make test
py.test
platform darwin -- Python 2.7.3 -- pytest-2.3.4
collected 25 items

test_requests.py .........................
25 passed in 3.50 seconds
</pre></div>
</div>
</div>
<div class="section" id="runtime-environments">
<h4>Runtime Environments<a class="headerlink" href="#runtime-environments" title="永久链接至标题">¶</a></h4>
<p>Requests currently supports the following versions of Python:</p>
<ul class="simple">
<li>Python 2.6</li>
<li>Python 2.7</li>
<li>Python 3.1</li>
<li>Python 3.2</li>
<li>Python 3.3</li>
<li>PyPy 1.9</li>
</ul>
<p>Support for Python 3.1 and 3.2 may be dropped at any time.</p>
<p>Google App Engine will never be officially supported. Pull requests for compatiblity will be accepted, as long as they don&#8217;t complicate the codebase.</p>
</div>
<div class="section" id="are-you-crazy">
<h4>Are you crazy?<a class="headerlink" href="#are-you-crazy" title="永久链接至标题">¶</a></h4>
<ul class="simple">
<li>SPDY support would be awesome. No C extensions.</li>
</ul>
</div>
</div>
<span id="document-dev/authors"></span><div class="section" id="id1">
<h3>贡献者<a class="headerlink" href="#id1" title="永久链接至标题">¶</a></h3>
<p>Requests is written and maintained by Kenneth Reitz and
various contributors:</p>
<div class="section" id="development-lead">
<h4>Development Lead<a class="headerlink" href="#development-lead" title="永久链接至标题">¶</a></h4>
<ul class="simple">
<li>Kenneth Reitz &lt;<a class="reference external" href="mailto:me&#37;&#52;&#48;kennethreitz&#46;com">me<span>&#64;</span>kennethreitz<span>&#46;</span>com</a>&gt;</li>
</ul>
</div>
<div class="section" id="urllib3">
<h4>Urllib3<a class="headerlink" href="#urllib3" title="永久链接至标题">¶</a></h4>
<ul class="simple">
<li>Andrey Petrov &lt;<a class="reference external" href="mailto:andrey&#46;petrov&#37;&#52;&#48;shazow&#46;net">andrey<span>&#46;</span>petrov<span>&#64;</span>shazow<span>&#46;</span>net</a>&gt;</li>
</ul>
</div>
<div class="section" id="patches-and-suggestions">
<h4>Patches and Suggestions<a class="headerlink" href="#patches-and-suggestions" title="永久链接至标题">¶</a></h4>
<ul class="simple">
<li>Various Pocoo Members</li>
<li>Chris Adams</li>
<li>Flavio Percoco Premoli</li>
<li>Dj Gilcrease</li>
<li>Justin Murphy</li>
<li>Rob Madole</li>
<li>Aram Dulyan</li>
<li>Johannes Gorset</li>
<li>村山めがね (Megane Murayama)</li>
<li>James Rowe</li>
<li>Daniel Schauenberg</li>
<li>Zbigniew Siciarz</li>
<li>Daniele Tricoli &#8216;Eriol&#8217;</li>
<li>Richard Boulton</li>
<li>Miguel Olivares &lt;<a class="reference external" href="mailto:miguel&#37;&#52;&#48;moliware&#46;com">miguel<span>&#64;</span>moliware<span>&#46;</span>com</a>&gt;</li>
<li>Alberto Paro</li>
<li>Jérémy Bethmont</li>
<li>潘旭 (Xu Pan)</li>
<li>Tamás Gulácsi</li>
<li>Rubén Abad</li>
<li>Peter Manser</li>
<li>Jeremy Selier</li>
<li>Jens Diemer</li>
<li>Alex &lt;&#64;alopatin&gt;</li>
<li>Tom Hogans &lt;<a class="reference external" href="mailto:tomhsx&#37;&#52;&#48;gmail&#46;com">tomhsx<span>&#64;</span>gmail<span>&#46;</span>com</a>&gt;</li>
<li>Armin Ronacher</li>
<li>Shrikant Sharat Kandula</li>
<li>Mikko Ohtamaa</li>
<li>Den Shabalin</li>
<li>Daniel Miller &lt;<a class="reference external" href="mailto:danielm&#37;&#52;&#48;vs-networks&#46;com">danielm<span>&#64;</span>vs-networks<span>&#46;</span>com</a>&gt;</li>
<li>Alejandro Giacometti</li>
<li>Rick Mak</li>
<li>Johan Bergström</li>
<li>Josselin Jacquard</li>
<li>Travis N. Vaught</li>
<li>Fredrik Möllerstrand</li>
<li>Daniel Hengeveld</li>
<li>Dan Head</li>
<li>Bruno Renié</li>
<li>David Fischer</li>
<li>Joseph McCullough</li>
<li>Juergen Brendel</li>
<li>Juan Riaza</li>
<li>Ryan Kelly</li>
<li>Rolando Espinoza La fuente</li>
<li>Robert Gieseke</li>
<li>Idan Gazit</li>
<li>Ed Summers</li>
<li>Chris Van Horne</li>
<li>Christopher Davis</li>
<li>Ori Livneh</li>
<li>Jason Emerick</li>
<li>Bryan Helmig</li>
<li>Jonas Obrist</li>
<li>Lucian Ursu</li>
<li>Tom Moertel</li>
<li>Frank Kumro Jr</li>
<li>Chase Sterling</li>
<li>Marty Alchin</li>
<li>takluyver</li>
<li>Ben Toews (mastahyeti)</li>
<li>David Kemp</li>
<li>Brendon Crawford</li>
<li>Denis (Telofy)</li>
<li>Cory Benfield (Lukasa)</li>
<li>Matt Giuca</li>
<li>Adam Tauber</li>
<li>Honza Javorek</li>
<li>Brendan Maguire &lt;<a class="reference external" href="mailto:maguire&#46;brendan&#37;&#52;&#48;gmail&#46;com">maguire<span>&#46;</span>brendan<span>&#64;</span>gmail<span>&#46;</span>com</a>&gt;</li>
<li>Chris Dary</li>
<li>Danver Braganza &lt;<a class="reference external" href="mailto:danverbraganza&#37;&#52;&#48;gmail&#46;com">danverbraganza<span>&#64;</span>gmail<span>&#46;</span>com</a>&gt;</li>
<li>Max Countryman</li>
<li>Nick Chadwick</li>
<li>Jonathan Drosdeck</li>
<li>Jiri Machalek</li>
<li>Steve Pulec</li>
<li>Michael Kelly</li>
<li>Michael Newman &lt;<a class="reference external" href="mailto:newmaniese&#37;&#52;&#48;gmail&#46;com">newmaniese<span>&#64;</span>gmail<span>&#46;</span>com</a>&gt;</li>
<li>Jonty Wareing &lt;<a class="reference external" href="mailto:jonty&#37;&#52;&#48;jonty&#46;co&#46;uk">jonty<span>&#64;</span>jonty<span>&#46;</span>co<span>&#46;</span>uk</a>&gt;</li>
<li>Shivaram Lingamneni</li>
<li>Miguel Turner</li>
<li>Rohan Jain (crodjer)</li>
<li>Justin Barber &lt;<a class="reference external" href="mailto:barber&#46;justin&#37;&#52;&#48;gmail&#46;com">barber<span>&#46;</span>justin<span>&#64;</span>gmail<span>&#46;</span>com</a>&gt;</li>
<li>Roman Haritonov &lt;&#64;reclosedev&gt;</li>
<li>Josh Imhoff &lt;<a class="reference external" href="mailto:joshimhoff13&#37;&#52;&#48;gmail&#46;com">joshimhoff13<span>&#64;</span>gmail<span>&#46;</span>com</a>&gt;</li>
<li>Arup Malakar &lt;<a class="reference external" href="mailto:amalakar&#37;&#52;&#48;gmail&#46;com">amalakar<span>&#64;</span>gmail<span>&#46;</span>com</a>&gt;</li>
<li>Danilo Bargen (dbrgn)</li>
<li>Torsten Landschoff</li>
<li>Michael Holler (apotheos)</li>
<li>Timnit Gebru</li>
<li>Sarah Gonzalez</li>
<li>Victoria Mo</li>
<li>Leila Muhtasib</li>
<li>Matthias Rahlf &lt;<a class="reference external" href="mailto:matthias&#37;&#52;&#48;webding&#46;de">matthias<span>&#64;</span>webding<span>&#46;</span>de</a>&gt;</li>
<li>Jakub Roztocil &lt;<a class="reference external" href="mailto:jakub&#37;&#52;&#48;roztocil&#46;name">jakub<span>&#64;</span>roztocil<span>&#46;</span>name</a>&gt;</li>
<li>Ian Cordasco &lt;<a class="reference external" href="mailto:graffatcolmingov&#37;&#52;&#48;gmail&#46;com">graffatcolmingov<span>&#64;</span>gmail<span>&#46;</span>com</a>&gt; &#64;sigmavirus24</li>
<li>Rhys Elsmore</li>
<li>André Graf (dergraf)</li>
<li>Stephen Zhuang (everbird)</li>
<li>Martijn Pieters</li>
<li>Jonatan Heyman</li>
<li>David Bonner &lt;<a class="reference external" href="mailto:dbonner&#37;&#52;&#48;gmail&#46;com">dbonner<span>&#64;</span>gmail<span>&#46;</span>com</a>&gt; &#64;rascalking</li>
<li>Vinod Chandru</li>
<li>Johnny Goodnow &lt;<a class="reference external" href="mailto:j&#46;goodnow29&#37;&#52;&#48;gmail&#46;com">j<span>&#46;</span>goodnow29<span>&#64;</span>gmail<span>&#46;</span>com</a>&gt;</li>
<li>Denis Ryzhkov &lt;<a class="reference external" href="mailto:denisr&#37;&#52;&#48;denisr&#46;com">denisr<span>&#64;</span>denisr<span>&#46;</span>com</a>&gt;</li>
<li>Wilfred Hughes &lt;<a class="reference external" href="mailto:me&#37;&#52;&#48;wilfred&#46;me&#46;uk">me<span>&#64;</span>wilfred<span>&#46;</span>me<span>&#46;</span>uk</a>&gt; &#64;dontYetKnow</li>
<li>Dmitry Medvinsky &lt;<a class="reference external" href="mailto:me&#37;&#52;&#48;dmedvinsky&#46;name">me<span>&#64;</span>dmedvinsky<span>&#46;</span>name</a>&gt;</li>
</ul>
</div>
</div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper"><p class="logo">
  <a href="index.html#document-index">
    <img class="logo" src="_static/requests-sidebar.png?@cache1" alt="Logo"/>
  </a>
</p>

<p>
</p>

<p>
  Requests is an elegant and simple HTTP library for Python, built for
  human beings. You are currently looking at the documentation of the
  development release.
</p>


<h3>Donate</h3>
<p>
    If you love Requests, consider supporting the author <a href="https://www.gittip.com/kennethreitz/">on Gittip</a>:
</p>
<p>
</p>

<p>
    If your organization uses Requests, consider financial support:

</p>
<p>

</p>

<h3>Feedback</h3>
<p>
  Feedback is greatly appreciated. If you have any questions, comments,
  random praise, or anonymous threats, <a href="mailto:me@kennethreitz.com">
  shoot me an email</a>.
</p>


<h3>Useful Links</h3>
<ul>
  <li><a href="http://pypi.python.org/pypi/requests">Requests @ PyPI</a></li>
  <li><a href="http://github.com/kennethreitz/requests">Requests @ GitHub</a></li>
  <li><a href="https://flattr.com/thing/442264/Requests">Requests @ Flattr</a></li>
  <li><a href="http://github.com/kennethreitz/requests/issues">Issue Tracker</a></li>
</ul>
<div id="searchbox" style="display: none" role="search">
  <h3>快速搜索</h3>
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="转向" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    输入相关的术语，模块，类或者函数名称进行搜索
    </p>
</div>

  </body>
</html>